Prev | Current Page 506 | Next

Kevin Marshall, Chad Pytel, and Jon Yurek

"Pro Active Record: Databases with Ruby and Rails"


Account.count # => SELECT COUNT(*) FROM accounts
This method also will take one or two strings as arguments, representing, respectively,
conditions for the WHERE part of the SQL query and a string to be added to the FROM part of the
query.
Account.count("site_id != 1") # SELECT COUNT(*) FROM accounts WHERE side_id != 1
Account.count("name != 'My Site'", "LEFT JOIN sites on accounts.site_id = sites.id")
# SELECT COUNT(*) FROM accounts LEFT JOIN sites on accounts.site_id = sites.id
# WHERE name != 'My Site'
Finally, this method also accepts arguments similar to the arguments to find, including
an options hash containing options for :conditions, :joins, :order, :group, :select, :having,
and :distinct.
Account.count(:conditions => "site_id != 1"
# SELECT COUNT(*) FROM accounts WHERE side_id != 1
Account.count(:conditions => "name != 'My Site'", :include => :site)
# SELECT COUNT(*) FROM accounts LEFT JOIN sites WHERE name != 'My Site'
Account.count('id', :conditions => "site_id != 1"
# SELECT COUNT(id) FROM accounts where site_id != 1
Account.


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