Browse files

Reword calculations section

  • Loading branch information...
1 parent d37a44e commit bab620fe5c33dbdb2e039f1aae921a2ef638f6be @lifo lifo committed with fxn Aug 30, 2010
Showing with 10 additions and 10 deletions.
  1. +10 −10 railties/guides/source/active_record_querying.textile
@@ -856,22 +856,24 @@ h3. Calculations
This section uses count as an example method in this preamble, but the options described apply to all sub-sections.
-<tt>count</tt> takes conditions much in the same way +exists?+ does:
+All calculation methods work directly on a model:
-Client.count(:conditions => "first_name = 'Ryan'")
+# SELECT count(*) AS count_all FROM clients
-Which will execute:
+Or on a relation :
-SELECT count(*) AS count_all FROM clients WHERE (first_name = 'Ryan')
+Client.where(:first_name => 'Ryan').count
+# SELECT count(*) AS count_all FROM clients WHERE (first_name = 'Ryan')
-You can also use the +includes+ or +joins+ methods for this to do something a little more complex:
+You can also use various finder methods on a relation for performing complex calculations:
-Client.where("clients.first_name = 'Ryan' AND orders.status = 'received'").includes("orders").count
+Client.includes("orders").where(:first_name => 'Ryan', :orders => {:status => 'received'}).count
Which will execute:
@@ -882,8 +884,6 @@ SELECT count(DISTINCT AS count_all FROM clients
(clients.first_name = 'Ryan' AND orders.status = 'received')
-This code specifies +clients.first_name+ just in case one of the join tables has a field also called +first_name+ and it uses +orders.status+ because that's the name of our join table.
h4. Count
If you want to see how many records are in your model's table you could call +Client.count+ and that will return the number. If you want to be more specific and find all the clients with their age present in the database you can use +Client.count(:age)+.

0 comments on commit bab620f

Please sign in to comment.