Module: BetterAr::Calculations

Defined in:
lib/better_ar/calculations.rb

Instance Method Summary collapse

Instance Method Details

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

Does a count on the query

example:

User.count(:age => 20)

is the same as:

User.where(:age => 20).count

if the key :conditions is present it will fall back to legacy

Parameters:

  • Optional (Hash)

    follows same convention as #all

Returns:

  • (Integer)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/better_ar/calculations.rb', line 16

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
    all(count_options).count
  end
end