aboutsummaryrefslogtreecommitdiffstats
path: root/railties/doc/guides/source/finders.txt
diff options
context:
space:
mode:
Diffstat (limited to 'railties/doc/guides/source/finders.txt')
-rw-r--r--railties/doc/guides/source/finders.txt44
1 files changed, 40 insertions, 4 deletions
diff --git a/railties/doc/guides/source/finders.txt b/railties/doc/guides/source/finders.txt
index 4c70c2b20b..d2bd55ada7 100644
--- a/railties/doc/guides/source/finders.txt
+++ b/railties/doc/guides/source/finders.txt
@@ -185,7 +185,7 @@ SELECT * FROM users WHERE (created_at IN
'2008-12-27','2008-12-28','2008-12-29','2008-12-30','2008-12-31'))
-------------------------------------------------------
-Things can get *really* messy if you pass in time objects as it will attempt to compare your field to *every second* in that range:
+Things can get *really* messy if you pass in Time objects as it will attempt to compare your field to *every second* in that range:
[source, ruby]
-------------------------------------------------------
@@ -224,7 +224,7 @@ Client.all(:conditions =>
["created_at >= ? AND created_at <= ?", params[:start_date], params[:end_date]])
-------------------------------------------------------
-Just like in Ruby.
+Just like in Ruby. If you want a shorter syntax be sure to check out the <<_hash_conditions, Hash Conditions>> section later on in the guide.
=== Placeholder Conditions ===
@@ -238,6 +238,40 @@ Client.all(:conditions =>
This makes for clearer readability if you have a large number of variable conditions.
+=== Hash Conditions
+
+Rails also allows you to pass in a hash conditions too which can increase the readability of your conditions syntax. With hash conditions, you pass in a hash with keys of the fields you want conditionalised and the values of how you want to conditionalise them:
+
+[source, ruby]
+-------------------------------------------------------
+Client.all(:conditions => { :locked => true })
+-------------------------------------------------------
+
+The field name does not have to be a symbol it can also be a string:
+
+[source, ruby]
+-------------------------------------------------------
+Client.all(:conditions => { 'locked' => true })
+-------------------------------------------------------
+
+The good thing about this is that we can pass in a range for our fields without it generating a large query as shown in the preamble of this section.
+
+[source, ruby]
+-------------------------------------------------------
+Client.all(:conditions => { :created_at => ((Time.now.midnight - 1.day)..Time.now.midnight})
+-------------------------------------------------------
+
+This will find all clients created yesterday. This shows the shorter syntax for the examples in <<_array_conditions, Array Conditions>>
+
+You can also join in tables and specify their columns in the hash:
+
+[source, ruby]
+-------------------------------------------------------
+Client.all(:include => "orders", :conditions => { 'orders.created_at; => ((Time.now.midnight - 1.day)..Time.now.midnight})
+-------------------------------------------------------
+
+This will find all clients who have orders that were created yesterday.
+
== Ordering
If you're getting a set of records and want to force an order, you can use +Client.all(:order => "created_at")+ which by default will sort the records by ascending order. If you'd like to order it in descending order, just tell it to do that using +Client.all(:order => "created_at desc")+
@@ -619,7 +653,7 @@ This code specifies +clients.first_name+ just in case one of the join tables has
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)+.
-For options, please see the parent section, Calculations.
+For options, please see the parent section, <<_calculations, Calculations>>.
=== Average
@@ -632,7 +666,7 @@ Client.average("orders_count")
This will return a number (possibly a floating point number such as 3.14159265) representing the average value in the field.
-For options, please see the parent section, <<_calculations, Calculations>>
+For options, please see the parent section, <<_calculations, Calculations>>.
=== Minimum
@@ -677,6 +711,8 @@ Thanks to Mike Gunderloy for his tips on creating this guide.
http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16[Lighthouse ticket]
+* December 17 2008: Fixed up syntax errors.
+* December 16 2008: Covered hash conditions that were introduced in Rails 2.2.2.
* December 1 2008: Added using an SQL function example to Selecting Certain Fields section as per http://rails.lighthouseapp.com/projects/16213/tickets/36-adding-an-example-for-using-distinct-to-ar-finders[this ticket]
* November 23 2008: Added documentation for +find_by_last+ and +find_by_bang!+
* November 21 2008: Fixed all points specified in http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16-activerecord-finders#ticket-16-13[this comment] and http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16-activerecord-finders#ticket-16-14[this comment]