Skip to content

Instantly share code, notes, and snippets.

@taq
Created March 16, 2009 13:24
Show Gist options
  • Save taq/79873 to your computer and use it in GitHub Desktop.
Save taq/79873 to your computer and use it in GitHub Desktop.
class ActiveRecord::Base
def self.find_by_sample(sample)
columns = self.columns.reject {|column| %w(id created_at updated_at).include?(column.name) }
cond_str, cond_val = [], []
columns.each do |column|
value = sample.send(column.name)
next if value.nil?
cond_str << "#{column.name}=?"
cond_val << value
end
self.all(:conditions=>[cond_str.join(" and "),*cond_val])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment