class Author < ActiveRecord::Base
  has_many :entries

  def self.find_or_create params
    # determine the columns (excluding 'id')
    @@cols ||= Author.columns.collect {|c| c.name}.reject {|n| n=="id"}

    # default name
    params[:name] ||= "anonymous"

    # build a query
    sql, values = [], []
    @@cols.each do |name|
      if params[name] == nil
        sql << "#{name} IS NULL"
      else
        sql << "#{name} = ?"
        values << params[name]
      end
    end

    # execute query
    author = Author.find :first, :conditions=>[sql.join(" AND ")]+values

    # if none found, create one
    unless author
      author = Author.new
      @@cols.each { |name| author[name] = params[name] }
    end

    return author
  end

end
