From 1e554a11758fcbe6ffcd929d98c02cdac9a37bbc Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Mon, 30 Aug 2010 23:12:58 +0100 Subject: Array conditions dont need [] --- railties/guides/source/active_record_querying.textile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index 178a5c50bf..95d992bd3e 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -234,7 +234,7 @@ h4. Array Conditions Now what if that number could vary, say as an argument from somewhere, or perhaps from the user's level status somewhere? The find then becomes something like: -Client.where(["orders_count = ?", params[:orders]]) +Client.where("orders_count = ?", params[:orders]) Active Record will go through the first element in the conditions value and any additional elements will replace the question marks +(?)+ in the first element. @@ -242,7 +242,7 @@ Active Record will go through the first element in the conditions value and any Or if you want to specify two conditions, you can do it like: -Client.where(["orders_count = ? AND locked = ?", params[:orders], false]) +Client.where("orders_count = ? AND locked = ?", params[:orders], false) In this example, the first question mark will be replaced with the value in +params[:orders]+ and the second will be replaced with the SQL representation of +false+, which depends on the adapter. @@ -250,7 +250,7 @@ In this example, the first question mark will be replaced with the value in +par The reason for doing code like: -Client.where(["orders_count = ?", params[:orders]]) +Client.where("orders_count = ?", params[:orders]) instead of: -- cgit v1.2.3 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 From ceca3a0564000024e2bbb273a431c3063f2011c5 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Mon, 30 Aug 2010 23:23:29 +0100 Subject: Improve example for having() --- railties/guides/source/active_record_querying.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index f93ff15a75..aa837f7ddf 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -496,7 +496,7 @@ SQL uses the +HAVING+ clause to specify conditions on the +GROUP BY+ fields. You For example: -Order.group("date(created_at)".having(["created_at > ?", 1.month.ago]) +Order.group("date(created_at)".having("created_at > ?", 1.month.ago) The SQL that would be executed would be something like this: -- cgit v1.2.3 From d0720ad5f8c49fdca103babf5f3d9df1aaae3af2 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Mon, 30 Aug 2010 23:33:46 +0100 Subject: Reword the section about exists? --- railties/guides/source/active_record_querying.textile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index aa837f7ddf..2451773990 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -838,13 +838,13 @@ Client.exists?(1,2,3) Client.exists?([1,2,3]) -The +exists+ method may also take a +conditions+ option much like find: +It's even possible to use +exists?+ without any arguments on a model or a relation. -Client.exists?(:conditions => "first_name = 'Ryan'") +Client.where(:first_name => 'Ryan').exists? -It's even possible to use +exists?+ without any arguments: +The above returns +true+ if there is at least one client with the +first_name+ 'Ryan' and +false+ otherwise. Client.exists? -- cgit v1.2.3 From 9cd708b2cf39219bff3ebcda1e4428403a88a02d Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Mon, 30 Aug 2010 23:44:51 +0100 Subject: Reword calculations section --- .../guides/source/active_record_querying.textile | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index 2451773990..b34e2ffc03 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/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. -count takes conditions much in the same way +exists?+ does: +All calculation methods work directly on a model: -Client.count(:conditions => "first_name = 'Ryan'") +Client.count +# 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 clients.id) 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)+. -- cgit v1.2.3 From 0bb32588b7e2badfa708271c0a743444abb76426 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Mon, 30 Aug 2010 23:52:27 +0100 Subject: Fix pessimistic locking examples --- railties/guides/source/active_record_querying.textile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index b34e2ffc03..291989c30e 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -571,13 +571,13 @@ end h5. Pessimistic Locking -Pessimistic locking uses a locking mechanism provided by the underlying database. Passing +:lock => true+ to +Model.find+ obtains an exclusive lock on the selected rows. +Model.find+ using +:lock+ are usually wrapped inside a transaction for preventing deadlock conditions. +Pessimistic locking uses a locking mechanism provided by the underlying database. Using +lock+ when building a relation obtains an exclusive lock on the selected rows. Relations using +lock+ are usually wrapped inside a transaction for preventing deadlock conditions. For example: Item.transaction do - i = Item.first(:lock => true) + i = Item.lock.first i.name = 'Jones' i.save end @@ -592,11 +592,11 @@ Item Update (0.4ms) UPDATE `items` SET `updated_at` = '2009-02-07 18:05:56', ` SQL (0.8ms) COMMIT -You can also pass raw SQL to the +:lock+ option to allow different types of locks. For example, MySQL has an expression called +LOCK IN SHARE MODE+ where you can lock a record but still allow other queries to read it. To specify this expression just pass it in as the lock option: +You can also pass raw SQL to the +lock+ method for allowing different types of locks. For example, MySQL has an expression called +LOCK IN SHARE MODE+ where you can lock a record but still allow other queries to read it. To specify this expression just pass it in as the lock option: Item.transaction do - i = Item.find(1, :lock => "LOCK IN SHARE MODE") + i = Item.lock("LOCK IN SHARE MODE").find(1) i.increment!(:views) end -- cgit v1.2.3 From 5a310f8335a407f2c95a84f1c73db20846370216 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Tue, 31 Aug 2010 00:01:41 +0100 Subject: Update the section about joins --- railties/guides/source/active_record_querying.textile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index 291989c30e..811f45d8b3 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -603,14 +603,14 @@ end h3. Joining Tables -Model.find provides a +:joins+ option for specifying +JOIN+ clauses on the resulting SQL. There are multiple ways to specify the +:joins+ option: +Active Record provides a finder method called +joins+ for specifying +JOIN+ clauses on the resulting SQL. There are multiple ways to use the +joins+ method. h4. Using a String SQL Fragment -You can just supply the raw SQL specifying the +JOIN+ clause to the +:joins+ option. For example: +You can just supply the raw SQL specifying the +JOIN+ clause to +joins+: -Client.all(:joins => 'LEFT OUTER JOIN addresses ON addresses.client_id = clients.id') +Client.joins('LEFT OUTER JOIN addresses ON addresses.client_id = clients.id') This will result in the following SQL: @@ -625,7 +625,7 @@ WARNING: This method only works with +INNER JOIN+,
-Active Record lets you use the names of the "associations":association_basics.html defined on the model as a shortcut for specifying the +:joins+ option. +Active Record lets you use the names of the "associations":association_basics.html defined on the model as a shortcut for specifying +JOIN+ clause for those associations when using the +joins+ method. For example, consider the following +Category+, +Post+, +Comments+ and +Guest+ models: -- cgit v1.2.3 From 6d8fc26ce860259d05d4655af584641e4bf33554 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Tue, 31 Aug 2010 00:14:49 +0100 Subject: Fix the readonly section --- railties/guides/source/active_record_querying.textile | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index 811f45d8b3..70a0ab9fcb 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -509,22 +509,16 @@ This will return single order objects for each day, but only for the last month. h4. Readonly Objects -To explicitly disallow modification/destruction of the matching records returned in a Relation object, you could chain the +readonly+ method as +true+ to the find call. - -Any attempt to alter or destroy the readonly records will not succeed, raising an +ActiveRecord::ReadOnlyRecord+ exception. To set this option, specify it like this: - - -Client.first.readonly(true) - - -For example, calling the following code will raise an +ActiveRecord::ReadOnlyRecord+ exception: +Active Record provides +readonly+ method on a relation to explicitly disallow modification or deletion of any of the returned object. Any attempt to alter or destroy a readonly record will not succeed, raising an +ActiveRecord::ReadOnlyRecord+ exception. -client = Client.first.readonly(true) -client.locked = false +client = Client.readonly.first +client.visits += 1 client.save +As +client+ is explicitly set to be a readonly object, the above code will raise an +ActiveRecord::ReadOnlyRecord+ exception when trying to calling +client.save+ with an updated value of _visists_. + h4. Locking Records for Update Locking is helpful for preventing race conditions when updating records in the database and ensuring atomic updates. -- cgit v1.2.3 From 5076af5aa19f019396d4d33b92813503c0a728ec Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Tue, 31 Aug 2010 00:16:31 +0100 Subject: Fix an english fail --- railties/guides/source/active_record_querying.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/guides/source/active_record_querying.textile b/railties/guides/source/active_record_querying.textile index 70a0ab9fcb..1de1808bc7 100644 --- a/railties/guides/source/active_record_querying.textile +++ b/railties/guides/source/active_record_querying.textile @@ -517,7 +517,7 @@ client.visits += 1 client.save
-As +client+ is explicitly set to be a readonly object, the above code will raise an +ActiveRecord::ReadOnlyRecord+ exception when trying to calling +client.save+ with an updated value of _visists_. +As +client+ is explicitly set to be a readonly object, the above code will raise an +ActiveRecord::ReadOnlyRecord+ exception when calling +client.save+ with an updated value of _visists_. h4. Locking Records for Update -- cgit v1.2.3 From 362371690ffd72fc6ef3bf46c23d38be6e1f4ba0 Mon Sep 17 00:00:00 2001 From: Michael MacDonald Date: Tue, 31 Aug 2010 11:01:05 +1000 Subject: Corrected typo in example on rendering collections in partials changing @posts to @products to match the example --- railties/guides/source/layouts_and_rendering.textile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/railties/guides/source/layouts_and_rendering.textile b/railties/guides/source/layouts_and_rendering.textile index fe5b4c8773..14107eb6f5 100644 --- a/railties/guides/source/layouts_and_rendering.textile +++ b/railties/guides/source/layouts_and_rendering.textile @@ -1087,7 +1087,7 @@ Partials are very useful in rendering collections. When you pass a collection to When a partial is called with a pluralized collection, then the individual instances of the partial have access to the member of the collection being rendered via a variable named after the partial. In this case, the partial is +_product+, and within the +_product+ partial, you can refer to +product+ to get the instance that is being rendered. -In Rails 3.0 there is also a shorthand for this, assuming +@posts+ is a collection of +post+ instances, you can simply do in the +index.html.erb+: +In Rails 3.0 there is also a shorthand for this, assuming +@products+ is a collection of +product+ instances, you can simply do in the +index.html.erb+:

Products

-- cgit v1.2.3 From c2ed4a9f88c9a5892dee4ed3dfde65e692763e9f Mon Sep 17 00:00:00 2001 From: Jaime Iniesta Date: Tue, 31 Aug 2010 09:19:43 +0200 Subject: Fix API docs for button_to options --- actionpack/lib/action_view/helpers/url_helper.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/action_view/helpers/url_helper.rb b/actionpack/lib/action_view/helpers/url_helper.rb index b0401c9859..56b38b57f4 100644 --- a/actionpack/lib/action_view/helpers/url_helper.rb +++ b/actionpack/lib/action_view/helpers/url_helper.rb @@ -269,8 +269,9 @@ module ActionView # The +options+ hash accepts the same options as url_for. # # There are a few special +html_options+: - # * :method - Specifies the anchor name to be appended to the path. - # * :disabled - Specifies the anchor name to be appended to the path. + # * :method - Symbol of HTTP verb. Supported verbs are :post, :get, + # :delete and :put. By default it will be :post. + # * :disabled - If set to true, it will generate a disabled button. # * :confirm - This will use the unobtrusive JavaScript driver to # prompt with the question specified. If the user accepts, the link is # processed normally, otherwise no action is taken. -- cgit v1.2.3 From 28474a7571b6a72f4a9433edf7c5212cf90c3938 Mon Sep 17 00:00:00 2001 From: Damien Mathieu Date: Tue, 31 Aug 2010 13:39:08 +0200 Subject: Fix benchmarking for rails3 * Fix the generated file examples * Fix the generators commands --- railties/guides/source/performance_testing.textile | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/railties/guides/source/performance_testing.textile b/railties/guides/source/performance_testing.textile index 7b21485ea0..9dda6d420a 100644 --- a/railties/guides/source/performance_testing.textile +++ b/railties/guides/source/performance_testing.textile @@ -20,7 +20,7 @@ In a freshly generated Rails application, +test/performance/browsing_test.rb+ co require 'test_helper' -require 'performance_test_help' +require 'rails/performance_test_help' # Profiling results for each test method are written to tmp/performance. class BrowsingTest < ActionController::PerformanceTest @@ -34,17 +34,17 @@ This example is a simple performance test case for profiling a GET request to th h4. Generating Performance Tests -Rails provides a generator called +performance_test+ for creating new performance tests: +Rails provides a generator called +test_unit:performance+ for creating new performance tests: -rails generate performance_test homepage +rails generate test_unit:performance homepage This generates +homepage_test.rb+ in the +test/performance+ directory: require 'test_helper' -require 'performance_test_help' +require 'rails/performance_test_help' class HomepageTest < ActionController::PerformanceTest # Replace this with your real tests. @@ -103,7 +103,7 @@ Here's the performance test for +HomeController#dashboard+ and +PostsController# require 'test_helper' -require 'performance_test_help' +require 'rails/performance_test_help' class PostPerformanceTest < ActionController::PerformanceTest def setup @@ -131,7 +131,7 @@ Performance test for +Post+ model: require 'test_helper' -require 'performance_test_help' +require 'rails/performance_test_help' class PostModelTest < ActionController::PerformanceTest def test_creation -- cgit v1.2.3