module Rails4Backports::ActiveRecordDynamicFinders

Public Instance Methods

find_by(*args) click to toggle source

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
# File lib/rails_4_backports/active_record_dynamic_finders.rb, line 14
def find_by(*args)
  where(*args).first
end
find_by!(*args) click to toggle source

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

# File lib/rails_4_backports/active_record_dynamic_finders.rb, line 20
def find_by!(*args)
  where(*args).first!
end