From 767eca491330d6ac1cd506a206e75664b60625c4 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Mon, 30 Aug 2010 23:19:43 +0100 Subject: Remove {} from hash conditions. And more occurrences of [] in array conditions --- .../guides/source/active_record_querying.textile | 24 +++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index 95d992bd3e..f93ff15a75 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/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: -Client.where( - ["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 Client.where( - ["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: Client.where( - ["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 => (Time.now.midnight - 1.day)..Time.now.midnight}) +Client.where(:created_at => (Time.now.midnight - 1.day)..Time.now.midnight) 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: -- cgit v1.2.3