Module: BetterAr::AssociationCollection

Defined in:
lib/better_ar/association_collection.rb

Instance Method Summary collapse

Instance Method Details

#count(column_name = nil, count_options = {}) ⇒ Object

Allows for the same interface as Relation#count on association collections

example:

User.first.records.count(:level => 2)

Parameters:

  • Optional (Hash)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/better_ar/association_collection.rb', line 29

def count(column_name = nil, count_options = {})
  if column_name.is_a?(Hash)
    count_options = column_name
    column_name = nil
  end

  if count_options.empty?
   super
  elsif count_options.key?(:conditions)
    super(column_name, count_options)
  else
    scope.where(count_options).count
  end
end

#first(opts = {}) ⇒ Object

Allows for the same interface as Relation#first on association collections

example:

User.first.records.first(:level => 2, :order! => :name)

Parameters:

  • Optional (Hash)


11
12
13
14
15
16
17
18
19
# File 'lib/better_ar/association_collection.rb', line 11

def first(opts = {})
  if opts.empty?
    super
  elsif opts.key?(:conditions)
    super(opts)
  else
    scoped.first(opts)
  end
end