Module: BetterAr::FinderMethods
- Defined in:
- lib/better_ar/finder_methods.rb
Instance Method Summary collapse
-
#all(opts = {}) ⇒ ActiveRecord::Relation
Breaks down the hash to do a ActiveRecord::Relation query.
-
#first(opts = {}) ⇒ ActiveRecord::Base
Forces a limit of 1 on the collection.
Instance Method Details
#all(opts = {}) ⇒ ActiveRecord::Relation
Breaks down the hash to do a ActiveRecord::Relation query
example:
User.all(:age => 10, :limit! => 2, :offset! => 3, :order! => :name)
is the same as:
User.where(:age => 10).limit(2).offset(3).order(:name)
if the key :conditions is present it will fall back to legacy
any key with the ‘!’ at the end will be assumed to be a sql operator. The key should match either an ActiveRecord::Relation instance method or an ARel predicate.
Implicit joins are supported. example:
User.all(:records => {:name => 'test'})
is the same as:
User.joins(:records).where(:records => {:name => 'test'})
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/better_ar/finder_methods.rb', line 25 def all(opts = {}) if opts.is_a?(Hash) if opts.empty? super() elsif opts.key?(:conditions) super(opts) else relation = clone relation = better_ar_apply_sql_clauses(relation, opts) relation = better_ar_apply_associations(relation, opts) relation.where(opts) end else relation = clone relation.where(opts) end end |
#first(opts = {}) ⇒ ActiveRecord::Base
Forces a limit of 1 on the collection
example:
User.first(:age => 10, :name => 'Brian')
is the same as:
User.where(:age => 10, :name => 'Brian').limit(1).first
if the key :conditions is present it will fall back to legacy
56 57 58 59 60 61 62 63 64 |
# File 'lib/better_ar/finder_methods.rb', line 56 def first(opts = {}) if opts.empty? super elsif opts.key?(:conditions) super(opts) else all(opts.merge(:limit! => 1)).first end end |