Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

Remove {} from hash conditions. And more occurrences of [] in array c…

  • Loading branch information...
commit 767eca491330d6ac1cd506a206e75664b60625c4 1 parent 1e554a1
@lifo lifo authored
Showing with 12 additions and 12 deletions.
  1. +12 −12 railties/guides/source/active_record_querying.textile
24 railties/guides/source/active_record_querying.textile
@@ -268,8 +268,8 @@ h5. Placeholder Conditions
Similar to the +(?)+ replacement style of params, you can also specify keys/values hash in your array conditions:
- ["created_at >= :start_date AND created_at <= :end_date", { :start_date => params[:start_date], :end_date => params[:end_date] }])
+Client.where("created_at >= :start_date AND created_at <= :end_date",
+ {:start_date => params[:start_date], :end_date => params[:end_date]})
This makes for clearer readability if you have a large number of variable conditions.
@@ -279,8 +279,8 @@ h5(#array-range_conditions). Range Conditions
If you're looking for a range inside of a table (for example, users created in a certain timeframe) you can use the conditions option coupled with the +IN+ SQL statement for this. If you had two dates coming in from a controller you could do something like this to look for a range:
-Client.where(["created_at IN (?)",
- (params[:start_date].to_date)..(params[:end_date].to_date)])
+Client.where("created_at IN (?)",
+ (params[:start_date].to_date)..(params[:end_date].to_date))
This would generate the proper query which is great for small ranges but not so good for larger ranges. For example if you pass in a range of date objects spanning a year that's 365 (or possibly 366, depending on the year) strings it will attempt to match your field against.
@@ -301,8 +301,8 @@ h5. Time and Date Conditions
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:
-Client.where(["created_at IN (?)",
- (params[:start_date].to_date.to_time)..(params[:end_date].to_date.to_time)])
+Client.where("created_at IN (?)",
+ (params[:start_date].to_date.to_time)..(params[:end_date].to_date.to_time))
@@ -323,14 +323,14 @@ In this example it would be better to use greater-than and less-than operators i
- ["created_at > ? AND created_at < ?", params[:start_date], params[:end_date]])
+ "created_at > ? AND created_at < ?", params[:start_date], params[:end_date])
You can also use the greater-than-or-equal-to and less-than-or-equal-to like this:
- ["created_at >= ? AND created_at <= ?", params[:start_date], params[:end_date]])
+ "created_at >= ? AND created_at <= ?", params[:start_date], params[:end_date])
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.
@@ -344,13 +344,13 @@ NOTE: Only equality, range and subset checking are possible with Hash conditions
h5. Equality Conditions
-Client.where({ :locked => true })
+Client.where(:locked => true)
The field name can also be a string:
-Client.where({ 'locked' => true })
+Client.where('locked' => true)
h5(#hash-range_conditions). Range Conditions
@@ -358,7 +358,7 @@ h5(#hash-range_conditions). Range Conditions
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.
-Client.where({ :created_at => ( -})
+Client.where(:created_at => ( -
This will find all clients created yesterday by using a +BETWEEN+ SQL statement:
@@ -374,7 +374,7 @@ h5. Subset Conditions
If you want to find records using the +IN+ expression you can pass an array to the conditions hash:
-Client.where({ :orders_count => [1,3,5] })
+Client.where(:orders_count => [1,3,5])
This code will generate SQL like this:
Please sign in to comment.
Something went wrong with that request. Please try again.