Module DataMapper::AggregateFunctions
In: lib/dm-aggregates/aggregate_functions.rb

Methods

aggregate   avg   count   max   min   sum  

Public Instance methods

Perform aggregate queries

@example the count of friends

  Friend.aggregate(:all.count)

@example the minimum age, the maximum age and the total age of friends

  Friend.aggregate(:age.min, :age.max, :age.sum)

@example the average age, grouped by gender

  Friend.aggregate(:age.avg, :fields => [ :gender ])

@param aggregates [Symbol, …] operators to aggregate with @params query [Hash] the conditions

@return [Array,Numeric,DateTime,Date,Time] the results of the

  aggregate query

@api public

Get the average value of a property

@example the average age of all friends

  Friend.avg(:age)

@example the average age of all female friends

  Friend.avg(:age, :conditions => [ 'gender = ?', 'female' ])

@param property [Symbol] the property you wish to get the average value of @param opts [Hash, Symbol] the conditions

@return [Integer] return the average value of a property given the conditions

@api public

Count results (given the conditions)

@example the count of all friends

  Friend.count

@example the count of all friends older then 18

  Friend.count(:age.gt => 18)

@example the count of all your female friends

  Friend.count(:conditions => [ 'gender = ?', 'female' ])

@example the count of all friends with an address (NULL values are not included)

  Friend.count(:address)

@example the count of all friends with an address that are older then 18

  Friend.count(:address, :age.gt => 18)

@example the count of all your female friends with an address

  Friend.count(:address, :conditions => [ 'gender = ?', 'female' ])

@param property [Symbol] of the property you with to count (optional) @param opts [Hash, Symbol] the conditions

@return [Integer] return the count given the conditions

@api public

Get the highest value of a property

@example the age of the oldest friend

  Friend.max(:age)

@example the age of the oldest female friend

  Friend.max(:age, :conditions => [ 'gender = ?', 'female' ])

@param property [Symbol] the property you wish to get the highest value of @param opts [Hash, Symbol] the conditions

@return [Integer] return the highest value of a property given the conditions

@api public

Get the lowest value of a property

@example the age of the youngest friend

  Friend.min(:age)

@example the age of the youngest female friend

  Friend.min(:age, :conditions => [ 'gender = ?', 'female' ])

@param property [Symbol] the property you wish to get the lowest value of @param opts [Hash, Symbol] the conditions

@return [Integer] return the lowest value of a property given the conditions

@api public

Get the total value of a property

@example the total age of all friends

  Friend.sum(:age)

@example the total age of all female friends

  Friend.max(:age, :conditions => [ 'gender = ?', 'female' ])

@param property [Symbol] the property you wish to get the total value of @param opts [Hash, Symbol] the conditions

@return [Integer] return the total value of a property given the conditions

@api public

[Validate]