Skip to content

Commit 9cd708b

Browse files
committed
Reword calculations section
1 parent d0720ad commit 9cd708b

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

railties/guides/source/active_record_querying.textile

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -856,22 +856,24 @@ h3. Calculations
856856

857857
This section uses count as an example method in this preamble, but the options described apply to all sub-sections.
858858

859-
<tt>count</tt> takes conditions much in the same way +exists?+ does:
859+
All calculation methods work directly on a model:
860860

861861
<ruby>
862-
Client.count(:conditions => "first_name = 'Ryan'")
862+
Client.count
863+
# SELECT count(*) AS count_all FROM clients
863864
</ruby>
864865

865-
Which will execute:
866+
Or on a relation :
866867

867-
<sql>
868-
SELECT count(*) AS count_all FROM clients WHERE (first_name = 'Ryan')
869-
</sql>
868+
<ruby>
869+
Client.where(:first_name => 'Ryan').count
870+
# SELECT count(*) AS count_all FROM clients WHERE (first_name = 'Ryan')
871+
</ruby>
870872

871-
You can also use the +includes+ or +joins+ methods for this to do something a little more complex:
873+
You can also use various finder methods on a relation for performing complex calculations:
872874

873875
<ruby>
874-
Client.where("clients.first_name = 'Ryan' AND orders.status = 'received'").includes("orders").count
876+
Client.includes("orders").where(:first_name => 'Ryan', :orders => {:status => 'received'}).count
875877
</ruby>
876878

877879
Which will execute:
@@ -882,8 +884,6 @@ SELECT count(DISTINCT clients.id) AS count_all FROM clients
882884
(clients.first_name = 'Ryan' AND orders.status = 'received')
883885
</sql>
884886

885-
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.
886-
887887
h4. Count
888888

889889
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 commit comments

Comments
 (0)