Module: Rails4Backports::ActiveRecordDynamicFinders

Defined in:
lib/rails_4_backports/active_record_dynamic_finders.rb

Instance Method Summary collapse

Instance Method Details

#find_by(*args) ⇒ Object

Finds the first record matching the specified conditions. There is no implied ording so if order matters, you should specify it yourself.

If no record is found, returns nil.

Post.find_by name: 'Spartacus', rating: 4
Post.find_by "published_at < ?", 2.weeks.ago


14
15
16
# File 'lib/rails_4_backports/active_record_dynamic_finders.rb', line 14

def find_by(*args)
  where(*args).first
end

#find_by!(*args) ⇒ Object

Like find_by, except that if no record is found, raises an ActiveRecord::RecordNotFound error.



20
21
22
# File 'lib/rails_4_backports/active_record_dynamic_finders.rb', line 20

def find_by!(*args)
  where(*args).first!
end