Module: BetterAr::AssociationCollection
- Defined in:
- lib/better_ar/association_collection.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#count_with_better_ar(opts = {}, attr = nil) ⇒ Object
Allows for the same interface as Relation#count on association collections.
-
#first_with_better_ar(opts = {}) ⇒ Object
Allows for the same interface as Relation#first on association collections.
Class Method Details
.included(base) ⇒ Object
4 5 6 7 8 9 |
# File 'lib/better_ar/association_collection.rb', line 4 def self.included(base) base.class_eval do alias_method_chain :first, :better_ar alias_method_chain :count, :better_ar end end |
Instance Method Details
#count_with_better_ar(opts = {}, attr = nil) ⇒ Object
Allows for the same interface as Relation#count on association collections
example:
User.first.records.count(:level => 2)
37 38 39 40 41 42 43 44 45 |
# File 'lib/better_ar/association_collection.rb', line 37 def count_with_better_ar(opts = {}, attr = nil) if opts.empty? count_without_better_ar elsif opts.key?(:conditions) count_without_better_ar(opts, attr) else scoped.count(opts) end end |
#first_with_better_ar(opts = {}) ⇒ Object
Allows for the same interface as Relation#first on association collections
example:
User.first.records.first(:level => 2, :order! => :name)
19 20 21 22 23 24 25 26 27 |
# File 'lib/better_ar/association_collection.rb', line 19 def first_with_better_ar(opts = {}) if opts.empty? first_without_better_ar elsif opts.key?(:conditions) first_without_better_ar(opts) else scoped.first(opts) end end |