Prev | Current Page 505 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"

..
# Select the minimum age for everyone with a last name other than 'Pytel'
Account.minimum(:age, :conditions => ['last_name != ?', 'Pytel'])
# Select the minimum age for any family without any minors
Account.minimum(:age, :having => 'min(age) > 17', :group => :last_name)
The :group option takes either the name of a column or the name of a belongs_to association,
and it causes the calculate method to return an ordered hash of the values, where the keys
are the grouped object:
values = Account.average(:age, :group => :last_name)
values["Pytel"] # => 42
this_site = Site.find(1)
values = Account.average(:age, :group_by => :site)
values[this_site] # => 42
APPENDIX ?–  ACTIVE RECORD METHODS IN DETAIL 231
The options valid to the calculate method are the same options valid to find, including
:conditions, :joins, :order, :group, :select, :having, and :distinct.
count(*args)
This method returns a Fixnum equal to the number of rows returned by the resulting database
query. If no arguments are passed in, this method will return the total number of rows in the
database table.


Pages:
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517