diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2009-01-11 19:22:19 +0000 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-01-11 19:22:19 +0000 |
commit | 55c18cfb28a69b73e4f720a61498ffe73fd6c22b (patch) | |
tree | 76a987ed3b5729df4ec9e6df103e63e5d9fc5b61 /railties/doc/guides/html | |
parent | bda787e9b10141c841ba3d86146d42b8c1e62a1b (diff) | |
download | rails-55c18cfb28a69b73e4f720a61498ffe73fd6c22b.tar.gz rails-55c18cfb28a69b73e4f720a61498ffe73fd6c22b.tar.bz2 rails-55c18cfb28a69b73e4f720a61498ffe73fd6c22b.zip |
- Add 'Performance test environment' to Performance guide
- Add rake middleware and Josh's chat log to Rack guide
- Remove long changelog from Active Record Finders guides
Diffstat (limited to 'railties/doc/guides/html')
-rw-r--r-- | railties/doc/guides/html/activerecord_validations_callbacks.html | 2 | ||||
-rw-r--r-- | railties/doc/guides/html/finders.html | 261 | ||||
-rw-r--r-- | railties/doc/guides/html/performance_testing.html | 27 | ||||
-rw-r--r-- | railties/doc/guides/html/rails_on_rack.html | 679 | ||||
-rw-r--r-- | railties/doc/guides/html/testing_rails_applications.html | 19 |
5 files changed, 469 insertions, 519 deletions
diff --git a/railties/doc/guides/html/activerecord_validations_callbacks.html b/railties/doc/guides/html/activerecord_validations_callbacks.html index 0862776f53..5251869119 100644 --- a/railties/doc/guides/html/activerecord_validations_callbacks.html +++ b/railties/doc/guides/html/activerecord_validations_callbacks.html @@ -645,7 +645,7 @@ http://www.gnu.org/software/src-highlite --> <td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content">If you want to validate the presence of a boolean field (where the real values are true and false), you will want to use validates_inclusion_of :field_name, :in ⇒ [true, false] This is due to the way Object#blank? handles boolean values. false.blank? # ⇒ true</td>
+<td class="content">If you want to validate the presence of a boolean field (where the real values are true and false), you will want to use validates_inclusion_of :field_name, :in => [true, false] This is due to the way Object#blank? handles boolean values. false.blank? # => true</td>
</tr></table>
</div>
<div class="paragraph"><p>The default error message for <tt>validates_presence_of</tt> is "<em>can’t be empty</em>".</p></div>
diff --git a/railties/doc/guides/html/finders.html b/railties/doc/guides/html/finders.html index ba766b71b9..ea5b06ec92 100644 --- a/railties/doc/guides/html/finders.html +++ b/railties/doc/guides/html/finders.html @@ -199,12 +199,6 @@ ul#navMain { <h2>Chapters</h2> <ol> <li> - <a href="#_the_sample_models">The Sample Models</a> - </li> - <li> - <a href="#_database_agnostic">Database Agnostic</a> - </li> - <li> <a href="#_ids_first_last_and_all">IDs, First, Last and All</a> </li> <li> @@ -298,9 +292,6 @@ ul#navMain { </ul> </li> <li> - <a href="#_credits">Credits</a> - </li> - <li> <a href="#_changelog">Changelog</a> </li> </ol> @@ -310,7 +301,7 @@ ul#navMain { <h1>Rails Finders</h1> <div id="preamble">
<div class="sectionbody">
-<div class="paragraph"><p>This guide covers the <tt>find</tt> method defined in ActiveRecord::Base, as well as other ways of finding particular instances of your models. By using this guide, you will be able to:</p></div>
+<div class="paragraph"><p>This guide covers different ways to retrieve data from the database using Active Record. By referring to this guide, you will be able to:</p></div>
<div class="ulist"><ul>
<li>
<p>
@@ -324,12 +315,12 @@ Specify the order, retrieved attributes, grouping, and other properties of the f </li>
<li>
<p>
-Use eager loading to cut down on the number of database queries in your application
+Use eager loading to reduce the number of database queries needed for data retrieval
</p>
</li>
<li>
<p>
-Use dynamic finders
+Use dynamic finders methods
</p>
</li>
<li>
@@ -344,17 +335,12 @@ Check for the existence of particular records </li>
<li>
<p>
-Perform aggregate calculations on Active Record models
+Perform various calculations on Active Record models
</p>
</li>
</ul></div>
<div class="paragraph"><p>If you’re used to using raw SQL to find database records then, generally, you will find that there are better ways to carry out the same operations in Rails. Active Record insulates you from the need to use SQL in most cases.</p></div>
-<div class="paragraph"><p>Generated SQL statements that appear in the log files may have their arguments quoted. Further, the form of quoting will be DBMS specific; SQLite3 will use "\ for example while MySQL will use `\ instead. Because of this, simply copying SQL from the examples contained within this guide may not work in your database system. Please consult the database systems manual before attempting to execute any SQL.</p></div>
-</div>
-</div>
-<h2 id="_the_sample_models">1. The Sample Models</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>This guide demonstrates finding using the following models:</p></div>
+<div class="paragraph"><p>Code examples in this guide use one or more of the following models:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -365,28 +351,45 @@ http://www.gnu.org/software/src-highlite --> has_one <span style="color: #990000">:</span>mailing_address
has_many <span style="color: #990000">:</span>orders
has_and_belongs_to_many <span style="color: #990000">:</span>roles
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Address <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Address <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
belongs_to <span style="color: #990000">:</span>client
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> MailingAddress <span style="color: #990000"><</span> Address
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> MailingAddress <span style="color: #990000"><</span> Address
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
belongs_to <span style="color: #990000">:</span>client<span style="color: #990000">,</span> <span style="color: #990000">:</span>counter_cache <span style="color: #990000">=></span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Role <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Role <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
has_and_belongs_to_many <span style="color: #990000">:</span>clients
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-</div>
-<h2 id="_database_agnostic">2. Database Agnostic</h2>
-<div class="sectionbody">
+<div class="sidebarblock">
+<div class="sidebar-content">
<div class="paragraph"><p>Active Record will perform queries on the database for you and is compatible with most database systems (MySQL, PostgreSQL and SQLite to name a few). Regardless of which database system you’re using, the Active Record method format will always be the same.</p></div>
+</div></div>
</div>
-<h2 id="_ids_first_last_and_all">3. IDs, First, Last and All</h2>
+</div>
+<h2 id="_ids_first_last_and_all">1. IDs, First, Last and All</h2>
<div class="sectionbody">
<div class="paragraph"><p>ActiveRecord::Base has methods defined on it to make interacting with your database and the tables within it much, much easier. For finding records, the key method is <tt>find</tt>. This method allows you to pass arguments into it to perform certain queries on your database without the need of SQL. If you wanted to find the record with the id of 1, you could type <tt>Client.find(1)</tt> which would execute this query on your database:</p></div>
<div class="listingblock">
@@ -482,10 +485,10 @@ http://www.gnu.org/software/src-highlite --> <div class="paragraph"><p>You may see in Rails code that there are calls to methods such as <tt>Client.find(:all)</tt>, <tt>Client.find(:first)</tt> and <tt>Client.find(:last)</tt>. These methods are just alternatives to <tt>Client.all</tt>, <tt>Client.first</tt> and <tt>Client.last</tt> respectively.</p></div>
<div class="paragraph"><p>Be aware that <tt>Client.first</tt>/<tt>Client.find(:first)</tt> and <tt>Client.last</tt>/<tt>Client.find(:last)</tt> will both return a single object, where as <tt>Client.all</tt>/<tt>Client.find(:all)</tt> will return an array of Client objects, just as passing in an array of ids to <tt>find</tt> will do also.</p></div>
</div>
-<h2 id="_conditions">4. Conditions</h2>
+<h2 id="_conditions">2. Conditions</h2>
<div class="sectionbody">
<div class="paragraph"><p>The <tt>find</tt> method allows you to specify conditions to limit the records returned. You can specify conditions as a string, array, or hash.</p></div>
-<h3 id="_pure_string_conditions">4.1. Pure String Conditions</h3>
+<h3 id="_pure_string_conditions">2.1. Pure String Conditions</h3>
<div class="paragraph"><p>If you’d like to add conditions to your find, you could just specify them in there, just like <tt>Client.first(:conditions => "orders_count = <em>2</em>")</tt>. This will find all clients where the <tt>orders_count</tt> field’s value is 2.</p></div>
<div class="admonitionblock">
<table><tr>
@@ -495,7 +498,7 @@ http://www.gnu.org/software/src-highlite --> <td class="content">Building your own conditions as pure strings can leave you vulnerable to SQL injection exploits. For example, <tt>Client.first(:conditions => "name LIKE <em>%#{params[:name]}%</em>")</tt> is not safe. See the next section for the preferred way to handle conditions using an array.</td>
</tr></table>
</div>
-<h3 id="_array_conditions">4.2. Array Conditions</h3>
+<h3 id="_array_conditions">2.2. Array Conditions</h3>
<div class="paragraph"><p>Now what if that number could vary, say as a argument from somewhere, or perhaps from the user’s level status somewhere? The find then becomes something like <tt>Client.first(:conditions => ["orders_count = ?", params[:orders]])</tt>. 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. If you want to specify two conditions, you can do it like <tt>Client.first(:conditions => ["orders_count = ? AND locked = ?", params[:orders], false])</tt>. In this example, the first question mark will be replaced with the value in <tt>params[:orders]</tt> and the second will be replaced with the SQL representation of <tt>false</tt>, which depends on the adapter.</p></div>
<div class="paragraph"><p>The reason for doing code like:</p></div>
<div class="listingblock">
@@ -581,7 +584,7 @@ http://www.gnu.org/software/src-highlite --> <pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=></span>
<span style="color: #990000">[</span><span style="color: #FF0000">"created_at >= ? AND created_at <= ?"</span><span style="color: #990000">,</span> params<span style="color: #990000">[:</span>start_date<span style="color: #990000">],</span> params<span style="color: #990000">[:</span>end_date<span style="color: #990000">]])</span></tt></pre></div></div>
<div class="paragraph"><p>Just like in Ruby. If you want a shorter syntax be sure to check out the <a href="#_hash_conditions">Hash Conditions</a> section later on in the guide.</p></div>
-<h3 id="_placeholder_conditions">4.3. Placeholder Conditions</h3>
+<h3 id="_placeholder_conditions">2.3. Placeholder Conditions</h3>
<div class="paragraph"><p>Similar to the array style of params you can also specify keys in your conditions:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
@@ -591,7 +594,7 @@ http://www.gnu.org/software/src-highlite --> <pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=></span>
<span style="color: #990000">[</span><span style="color: #FF0000">"created_at >= :start_date AND created_at <= :end_date"</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>start_date <span style="color: #990000">=></span> params<span style="color: #990000">[:</span>start_date<span style="color: #990000">],</span> <span style="color: #990000">:</span>end_date <span style="color: #990000">=></span> params<span style="color: #990000">[:</span>end_date<span style="color: #990000">]</span> <span style="color: #FF0000">}</span><span style="color: #990000">])</span></tt></pre></div></div>
<div class="paragraph"><p>This makes for clearer readability if you have a large number of variable conditions.</p></div>
-<h3 id="_hash_conditions">4.4. Hash Conditions</h3>
+<h3 id="_hash_conditions">2.4. Hash Conditions</h3>
<div class="paragraph"><p>Rails also allows you to pass in a hash conditions 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:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
@@ -651,11 +654,11 @@ http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> <span style="color: #FF0000">`clients`</span> <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span><span style="color: #FF0000">`clients`</span><span style="color: #990000">.</span><span style="color: #FF0000">`orders_count`</span> <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span> <span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span><span style="color: #993399">2</span><span style="color: #990000">,</span><span style="color: #993399">3</span><span style="color: #990000">))</span></tt></pre></div></div>
</div>
-<h2 id="_ordering">5. Ordering</h2>
+<h2 id="_ordering">3. Ordering</h2>
<div class="sectionbody">
<div class="paragraph"><p>If you’re getting a set of records and want to order them in ascending order by the <tt>created_at</tt> field in your table, you can use <tt>Client.all(:order => "created_at")</tt>. If you’d like to order it in descending order, just tell it to do that using <tt>Client.all(:order => "created_at desc")</tt>. The value for this option is passed in as sanitized SQL and allows you to sort via multiple fields: <tt>Client.all(:order => "created_at desc, orders_count asc")</tt>.</p></div>
</div>
-<h2 id="_selecting_certain_fields">6. Selecting Certain Fields</h2>
+<h2 id="_selecting_certain_fields">4. Selecting Certain Fields</h2>
<div class="sectionbody">
<div class="paragraph"><p>To select certain fields, you can use the select option like this: <tt>Client.first(:select => "viewable_by, locked")</tt>. This select option does not use an array of fields, but rather requires you to type SQL-like code. The above code will execute <tt>SELECT viewable_by, locked FROM clients LIMIT 1</tt> on your database.</p></div>
<div class="paragraph"><p>Be careful because this also means you’re initializing a model object with only the fields that you’ve selected. If you attempt to access a field that is not in the initialized record you’ll receive:</p></div>
@@ -666,7 +669,7 @@ http://www.gnu.org/software/src-highlite --> <div class="paragraph"><p>Where <attribute> is the atrribute you asked for. The <tt>id</tt> method will not raise the <tt>ActiveRecord::MissingAttributeError</tt>, so just be careful when working with associations because they need the <tt>id</tt> method to function properly.</p></div>
<div class="paragraph"><p>You can also call SQL functions within the select option. For example, if you would like to only grab a single record per unique value in a certain field by using the <tt>DISTINCT</tt> function you can do it like this: <tt>Client.all(:select => "DISTINCT(name)")</tt>.</p></div>
</div>
-<h2 id="_limit_amp_offset">7. Limit & Offset</h2>
+<h2 id="_limit_amp_offset">5. Limit & Offset</h2>
<div class="sectionbody">
<div class="paragraph"><p>If you want to limit the amount of records to a certain subset of all the records retrieved you usually use limit for this, sometimes coupled with offset. Limit is the maximum number of records that will be retrieved from a query, and offset is the number of records it will start reading from from the first record of the set. Take this code for example:</p></div>
<div class="listingblock">
@@ -696,7 +699,7 @@ http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">5</span><span style="color: #990000">,</span> <span style="color: #993399">5</span></tt></pre></div></div>
</div>
-<h2 id="_group">8. Group</h2>
+<h2 id="_group">6. Group</h2>
<div class="sectionbody">
<div class="paragraph"><p>The group option for find is useful, for example, if you want to find a collection of the dates orders were created on. You could use the option in this context:</p></div>
<div class="listingblock">
@@ -714,7 +717,7 @@ http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> orders <span style="font-weight: bold"><span style="color: #0000FF">GROUP</span></span> <span style="font-weight: bold"><span style="color: #0000FF">BY</span></span> <span style="color: #009900">date</span><span style="color: #990000">(</span>created_at<span style="color: #990000">)</span></tt></pre></div></div>
</div>
-<h2 id="_having">9. Having</h2>
+<h2 id="_having">7. Having</h2>
<div class="sectionbody">
<div class="paragraph"><p>The <tt>:having</tt> option allows you to specify SQL and acts as a kind of a filter on the group option. <tt>:having</tt> can only be specified when <tt>:group</tt> is specified.</p></div>
<div class="paragraph"><p>An example of using it would be:</p></div>
@@ -726,7 +729,7 @@ http://www.gnu.org/software/src-highlite --> <pre><tt>Order<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>group <span style="color: #990000">=></span> <span style="color: #FF0000">"date(created_at)"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>having <span style="color: #990000">=></span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at > ?"</span><span style="color: #990000">,</span> <span style="color: #993399">1</span><span style="color: #990000">.</span>month<span style="color: #990000">.</span>ago<span style="color: #990000">])</span></tt></pre></div></div>
<div class="paragraph"><p>This will return single order objects for each day, but only for the last month.</p></div>
</div>
-<h2 id="_read_only">10. Read Only</h2>
+<h2 id="_read_only">8. Read Only</h2>
<div class="sectionbody">
<div class="paragraph"><p><tt>readonly</tt> is a <tt>find</tt> option that you can set in order to make that instance of the record read-only. Any attempt to alter or destroy the record will not succeed, raising an ActiveRecord::ReadOnlyRecord exception. To set this option, specify it like this:</p></div>
<div class="listingblock">
@@ -745,7 +748,7 @@ http://www.gnu.org/software/src-highlite --> client<span style="color: #990000">.</span>locked <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span>
client<span style="color: #990000">.</span>save</tt></pre></div></div>
</div>
-<h2 id="_lock">11. Lock</h2>
+<h2 id="_lock">9. Lock</h2>
<div class="sectionbody">
<div class="paragraph"><p>If you’re wanting to stop race conditions for a specific record (for example, you’re incrementing a single field for a record, potentially from multiple simultaneous connections) you can use the lock option to ensure that the record is updated correctly. For safety, you should use this inside a transaction.</p></div>
<div class="listingblock">
@@ -768,11 +771,11 @@ http://www.gnu.org/software/src-highlite --> t<span style="color: #990000">.</span>increment!<span style="color: #990000">(:</span>views<span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
</div>
-<h2 id="_making_it_all_work_together">12. Making It All Work Together</h2>
+<h2 id="_making_it_all_work_together">10. Making It All Work Together</h2>
<div class="sectionbody">
<div class="paragraph"><p>You can chain these options together in no particular order as Active Record will write the correct SQL for you. If you specify two instances of the same options inside the <tt>find</tt> method Active Record will use the last one you specified. This is because the options passed to find are a hash and defining the same key twice in a hash will result in the last definition being used.</p></div>
</div>
-<h2 id="_eager_loading">13. Eager Loading</h2>
+<h2 id="_eager_loading">11. Eager Loading</h2>
<div class="sectionbody">
<div class="paragraph"><p>Eager loading is loading associated records along with any number of records in as few queries as possible. For example, if you wanted to load all the addresses associated with all the clients in a single query you could use <tt>Client.all(:include => :address)</tt>. If you wanted to include both the address and mailing address for the client you would use <tt>Client.find(:all, :include => [:address, :mailing_address])</tt>. Include will first find the client records and then load the associated address records. Running script/server in one window, and executing the code through script/console in another window, the output should look similar to this:</p></div>
<div class="listingblock">
@@ -814,7 +817,7 @@ http://www.gnu.org/software/src-highlite --> <pre><tt>Client<span style="color: #990000">.</span>first<span style="color: #990000">(:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span> <span style="color: #990000">=></span> <span style="color: #FF0000">"orders"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=></span>
<span style="color: #990000">[</span><span style="color: #FF0000">"orders.created_at >= ? AND orders.created_at <= ?"</span><span style="color: #990000">,</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>weeks<span style="color: #990000">.</span>ago<span style="color: #990000">,</span> Time<span style="color: #990000">.</span>now<span style="color: #990000">])</span></tt></pre></div></div>
</div>
-<h2 id="_dynamic_finders">14. Dynamic finders</h2>
+<h2 id="_dynamic_finders">12. Dynamic finders</h2>
<div class="sectionbody">
<div class="paragraph"><p>For every field (also known as an attribute) you define in your table, Active Record provides a finder method. If you have a field called <tt>name</tt> on your Client model for example, you get <tt>find_by_name</tt> and <tt>find_all_by_name</tt> for free from Active Record. If you have also have a <tt>locked</tt> field on the Client model, you also get <tt>find_by_locked</tt> and <tt>find_all_by_locked</tt>.</p></div>
<div class="paragraph"><p>You can do <tt>find_last_by_*</tt> methods too which will find the last record matching your argument.</p></div>
@@ -840,7 +843,7 @@ http://www.gnu.org/software/src-highlite --> <pre><tt>client <span style="color: #990000">=</span> Client<span style="color: #990000">.</span>find_or_initialize_by_name<span style="color: #990000">(</span><span style="color: #FF0000">'Ryan'</span><span style="color: #990000">)</span></tt></pre></div></div>
<div class="paragraph"><p>will either assign an existing client object with the name <em>Ryan</em> to the client local variable, or initialize a new object similar to calling <tt>Client.new(:name => <em>Ryan</em>)</tt>. From here, you can modify other fields in client by calling the attribute setters on it: <tt>client.locked = true</tt> and when you want to write it to the database just call <tt>save</tt> on it.</p></div>
</div>
-<h2 id="_finding_by_sql">15. Finding By SQL</h2>
+<h2 id="_finding_by_sql">13. Finding By SQL</h2>
<div class="sectionbody">
<div class="paragraph"><p>If you’d like to use your own SQL to find records in a table you can use <tt>find_by_sql</tt>. The <tt>find_by_sql</tt> method will return an array of objects even the underlying query returns just a single record. For example you could run this query:</p></div>
<div class="listingblock">
@@ -851,7 +854,7 @@ http://www.gnu.org/software/src-highlite --> <pre><tt>Client<span style="color: #990000">.</span>find_by_sql<span style="color: #990000">(</span><span style="color: #FF0000">"SELECT * FROM clients INNER JOIN orders ON clients.id = orders.client_id ORDER clients.created_at desc"</span><span style="color: #990000">)</span></tt></pre></div></div>
<div class="paragraph"><p><tt>find_by_sql</tt> provides you with a simple way of making custom calls to the database and retrieving instantiated objects.</p></div>
</div>
-<h2 id="_tt_select_all_tt">16. <tt>select_all</tt></h2>
+<h2 id="_tt_select_all_tt">14. <tt>select_all</tt></h2>
<div class="sectionbody">
<div class="paragraph"><p><tt>find_by_sql</tt> has a close relative called <tt>connection#select_all</tt>. <tt>select_all</tt> will retrieve objects from the database using custom SQL just like <tt>find_by_sql</tt> but will not instantiate them. Instead, you will get an array of hashes where each hash indicates a record.</p></div>
<div class="listingblock">
@@ -861,14 +864,14 @@ http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite -->
<pre><tt>Client<span style="color: #990000">.</span>connection<span style="color: #990000">.</span>select_all<span style="color: #990000">(</span><span style="color: #FF0000">"SELECT * FROM `clients` WHERE `id` = '1'"</span><span style="color: #990000">)</span></tt></pre></div></div>
</div>
-<h2 id="_working_with_associations">17. Working with Associations</h2>
+<h2 id="_working_with_associations">15. Working with Associations</h2>
<div class="sectionbody">
<div class="paragraph"><p>When you define a has_many association on a model you get the <tt>find</tt> method and dynamic finders also on that association. This is helpful for finding associated records within the scope of an existing record, for example finding all the orders for a client that have been sent and not received by doing something like <tt>Client.find(params[:id]).orders.find_by_sent_and_received(true, false)</tt>. Having this find method available on associations is extremely helpful when using nested resources.</p></div>
</div>
-<h2 id="_named_scopes">18. Named Scopes</h2>
+<h2 id="_named_scopes">16. Named Scopes</h2>
<div class="sectionbody">
<div class="paragraph"><p>Named scopes are another way to add custom finding behavior to the models in the application. Named scopes provide an object-oriented way to narrow the results of a query.</p></div>
-<h3 id="_simple_named_scopes">18.1. Simple Named Scopes</h3>
+<h3 id="_simple_named_scopes">16.1. Simple Named Scopes</h3>
<div class="paragraph"><p>Suppose we want to find all clients who are male. You could use this code:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
@@ -889,7 +892,7 @@ http://www.gnu.org/software/src-highlite --> named_scope <span style="color: #990000">:</span>active<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=></span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>active <span style="color: #990000">=></span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #FF0000">}</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="paragraph"><p>You can call this new named_scope with <tt>Client.active.all</tt> and this will do the same query as if we just used <tt>Client.all(:conditions => ["active = ?", true])</tt>. If you want to find the first client within this named scope you could do <tt>Client.active.first</tt>.</p></div>
-<h3 id="_combining_named_scopes">18.2. Combining Named Scopes</h3>
+<h3 id="_combining_named_scopes">16.2. Combining Named Scopes</h3>
<div class="paragraph"><p>If you wanted to find all the clients who are active and male you can stack the named scopes like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
@@ -904,7 +907,7 @@ by Lorenzo Bettini http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>Client<span style="color: #990000">.</span>males<span style="color: #990000">.</span>active<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=></span> <span style="color: #990000">[</span><span style="color: #FF0000">"age > ?"</span><span style="color: #990000">,</span> params<span style="color: #990000">[:</span>age<span style="color: #990000">]])</span></tt></pre></div></div>
-<h3 id="_runtime_evaluation_of_named_scope_conditions">18.3. Runtime Evaluation of Named Scope Conditions</h3>
+<h3 id="_runtime_evaluation_of_named_scope_conditions">16.3. Runtime Evaluation of Named Scope Conditions</h3>
<div class="paragraph"><p>Consider the following code:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
@@ -924,7 +927,7 @@ http://www.gnu.org/software/src-highlite --> named_scope <span style="color: #990000">:</span>recent<span style="color: #990000">,</span> lambda <span style="color: #FF0000">{</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=></span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at > ?"</span><span style="color: #990000">,</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>weeks<span style="color: #990000">.</span>ago<span style="color: #990000">]</span> <span style="color: #FF0000">}</span> <span style="color: #FF0000">}</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="paragraph"><p>And now every time the <tt>recent</tt> named scope is called, the code in the lambda block will be executed, so you’ll get actually 2 weeks ago from the code execution, not 2 weeks ago from the time the model was loaded.</p></div>
-<h3 id="_named_scopes_with_multiple_models">18.4. Named Scopes with Multiple Models</h3>
+<h3 id="_named_scopes_with_multiple_models">16.4. Named Scopes with Multiple Models</h3>
<div class="paragraph"><p>In a named scope you can use <tt>:include</tt> and <tt>:joins</tt> options just like in <tt>find</tt>.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
@@ -936,7 +939,7 @@ http://www.gnu.org/software/src-highlite --> lambda <span style="color: #FF0000">{</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=></span> <span style="color: #990000">[</span><span style="color: #FF0000">"orders.created_at > ?"</span><span style="color: #990000">,</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>weeks<span style="color: #990000">.</span>ago<span style="color: #990000">]</span> <span style="color: #FF0000">}</span> <span style="color: #FF0000">}</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="paragraph"><p>This method, called as <tt>Client.active_within_2_weeks.all</tt>, will return all clients who have placed orders in the past 2 weeks.</p></div>
-<h3 id="_arguments_to_named_scopes">18.5. Arguments to Named Scopes</h3>
+<h3 id="_arguments_to_named_scopes">16.5. Arguments to Named Scopes</h3>
<div class="paragraph"><p>If you want to pass to a named scope a required arugment, just specify it as a block argument like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
@@ -957,7 +960,7 @@ http://www.gnu.org/software/src-highlite --> <span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="paragraph"><p>This will work with <tt>Client.recent(2.weeks.ago).all</tt> and <tt>Client.recent.all</tt>, with the latter always returning records with a created_at date between right now and 2 weeks ago.</p></div>
<div class="paragraph"><p>Remember that named scopes are stackable, so you will be able to do <tt>Client.recent(2.weeks.ago).unlocked.all</tt> to find all clients created between right now and 2 weeks ago and have their locked field set to false.</p></div>
-<h3 id="_anonymous_scopes">18.6. Anonymous Scopes</h3>
+<h3 id="_anonymous_scopes">16.6. Anonymous Scopes</h3>
<div class="paragraph"><p>All Active Record models come with a named scope named <tt>scoped</tt>, which allows you to create anonymous scopes. For example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
@@ -978,7 +981,7 @@ http://www.gnu.org/software/src-highlite --> <pre><tt>Client<span style="color: #990000">.</span>scoped<span style="color: #990000">(:</span>conditions <span style="color: #990000">=></span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>gender <span style="color: #990000">=></span> <span style="color: #FF0000">"male"</span> <span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
<div class="paragraph"><p>Just like named scopes, anonymous scopes can be stacked, either with other anonymous scopes or with regular named scopes.</p></div>
</div>
-<h2 id="_existence_of_objects">19. Existence of Objects</h2>
+<h2 id="_existence_of_objects">17. Existence of Objects</h2>
<div class="sectionbody">
<div class="paragraph"><p>If you simply want to check for the existence of the object there’s a method called <tt>exists?</tt>. This method will query the database using the same query as <tt>find</tt>, but instead of returning an object or collection of objects it will return either <tt>true</tt> or false+.</p></div>
<div class="listingblock">
@@ -1004,7 +1007,7 @@ http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite -->
<pre><tt>Client<span style="color: #990000">.</span>exists?<span style="color: #990000">(:</span>conditions <span style="color: #990000">=></span> <span style="color: #FF0000">"first_name = 'Ryan'"</span><span style="color: #990000">)</span></tt></pre></div></div>
</div>
-<h2 id="_calculations">20. Calculations</h2>
+<h2 id="_calculations">18. Calculations</h2>
<div class="sectionbody">
<div class="paragraph"><p>This section uses count as an example method in this preamble, but the options described apply to all sub-sections.</p></div>
<div class="paragraph"><p><tt>count</tt> takes conditions much in the same way <tt>exists?</tt> does:</p></div>
@@ -1038,10 +1041,10 @@ http://www.gnu.org/software/src-highlite --> <span style="font-weight: bold"><span style="color: #0000FF">LEFT</span></span> <span style="font-weight: bold"><span style="color: #0000FF">OUTER</span></span> <span style="font-weight: bold"><span style="color: #0000FF">JOIN</span></span> orders <span style="font-weight: bold"><span style="color: #0000FF">ON</span></span> orders<span style="color: #990000">.</span>client_id <span style="color: #990000">=</span> client<span style="color: #990000">.</span>id <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span>
<span style="color: #990000">(</span>clients<span style="color: #990000">.</span>first_name <span style="color: #990000">=</span> <span style="color: #FF0000">'Ryan'</span> <span style="font-weight: bold"><span style="color: #0000FF">AND</span></span> orders<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">status</span></span> <span style="color: #990000">=</span> <span style="color: #FF0000">'received'</span><span style="color: #990000">)</span></tt></pre></div></div>
<div class="paragraph"><p>This code specifies <tt>clients.first_name</tt> just in case one of the join tables has a field also called <tt>first_name</tt> and it uses <tt>orders.status</tt> because that’s the name of our join table.</p></div>
-<h3 id="_count">20.1. Count</h3>
+<h3 id="_count">18.1. Count</h3>
<div class="paragraph"><p>If you want to see how many records are in your model’s table you could call <tt>Client.count</tt> 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 <tt>Client.count(:age)</tt>.</p></div>
<div class="paragraph"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a>.</p></div>
-<h3 id="_average">20.2. Average</h3>
+<h3 id="_average">18.2. Average</h3>
<div class="paragraph"><p>If you want to see the average of a certain number in one of your tables you can call the <tt>average</tt> method on the class that relates to the table. This method call will look something like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
@@ -1051,7 +1054,7 @@ http://www.gnu.org/software/src-highlite --> <pre><tt>Client<span style="color: #990000">.</span>average<span style="color: #990000">(</span><span style="color: #FF0000">"orders_count"</span><span style="color: #990000">)</span></tt></pre></div></div>
<div class="paragraph"><p>This will return a number (possibly a floating point number such as 3.14159265) representing the average value in the field.</p></div>
<div class="paragraph"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a>.</p></div>
-<h3 id="_minimum">20.3. Minimum</h3>
+<h3 id="_minimum">18.3. Minimum</h3>
<div class="paragraph"><p>If you want to find the minimum value of a field in your table you can call the <tt>minimum</tt> method on the class that relates to the table. This method call will look something like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
@@ -1060,7 +1063,7 @@ http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite -->
<pre><tt>Client<span style="color: #990000">.</span>minimum<span style="color: #990000">(</span><span style="color: #FF0000">"age"</span><span style="color: #990000">)</span></tt></pre></div></div>
<div class="paragraph"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a></p></div>
-<h3 id="_maximum">20.4. Maximum</h3>
+<h3 id="_maximum">18.4. Maximum</h3>
<div class="paragraph"><p>If you want to find the maximum value of a field in your table you can call the <tt>maximum</tt> method on the class that relates to the table. This method call will look something like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
@@ -1069,7 +1072,7 @@ http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite -->
<pre><tt>Client<span style="color: #990000">.</span>maximum<span style="color: #990000">(</span><span style="color: #FF0000">"age"</span><span style="color: #990000">)</span></tt></pre></div></div>
<div class="paragraph"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a></p></div>
-<h3 id="_sum">20.5. Sum</h3>
+<h3 id="_sum">18.5. Sum</h3>
<div class="paragraph"><p>If you want to find the sum of a field for all records in your table you can call the <tt>sum</tt> method on the class that relates to the table. This method call will look something like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
@@ -1079,133 +1082,13 @@ http://www.gnu.org/software/src-highlite --> <pre><tt>Client<span style="color: #990000">.</span>sum<span style="color: #990000">(</span><span style="color: #FF0000">"orders_count"</span><span style="color: #990000">)</span></tt></pre></div></div>
<div class="paragraph"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a></p></div>
</div>
-<h2 id="_credits">21. Credits</h2>
-<div class="sectionbody">
-<div class="paragraph"><p>Thanks to Ryan Bates for his awesome screencast on named scope #108. The information within the named scope section is intentionally similar to it, and without the cast may have not been possible.</p></div>
-<div class="paragraph"><p>Thanks to Mike Gunderloy for his tips on creating this guide.</p></div>
-</div>
-<h2 id="_changelog">22. Changelog</h2>
+<h2 id="_changelog">19. Changelog</h2>
<div class="sectionbody">
<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16">Lighthouse ticket</a></p></div>
<div class="ulist"><ul>
<li>
<p>
-December 29 2008: Added <a href="http://rails.lighthouseapp.com/projects/16213/tickets/16-activerecord-finders#ticket-16-32">James B. Byrne’s suggestions from this ticket</a>
-</p>
-</li>
-<li>
-<p>
-December 23 2008: Xavier Noria suggestions added! From <a href="http://rails.lighthouseapp.com/projects/16213/tickets/16-activerecord-finders#ticket-16-27">this ticket</a> and <a href="http://rails.lighthouseapp.com/projects/16213/tickets/16-activerecord-finders#ticket-16-28">this ticket</a> and <a href="http://rails.lighthouseapp.com/projects/16213/tickets/16-activerecord-finders#ticket-16-29">this ticket</a>
-</p>
-</li>
-<li>
-<p>
-December 22 2008: Added section on having.
-</p>
-</li>
-<li>
-<p>
-December 22 2008: Added description of how to make hash conditions use an IN expression <a href="http://rails.loglibrary.com/chats/15279234">mentioned here</a>
-</p>
-</li>
-<li>
-<p>
-December 22 2008: Mentioned using SQL as values for the lock option as mentioned in <a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16-activerecord-finders#ticket-16-24">this ticket</a>
-</p>
-</li>
-<li>
-<p>
-December 21 2008: Fixed <a href="http://rails.lighthouseapp.com/projects/16213/tickets/16-activerecord-finders#ticket-16-22">this ticket</a> minus two points; the lock SQL syntax and the having option.
-</p>
-</li>
-<li>
-<p>
-December 21 2008: Added more to the has conditions section.
-</p>
-</li>
-<li>
-<p>
-December 17 2008: Fixed up syntax errors.
-</p>
-</li>
-<li>
-<p>
-December 16 2008: Covered hash conditions that were introduced in Rails 2.2.2.
-</p>
-</li>
-<li>
-<p>
-December 1 2008: Added using an SQL function example to Selecting Certain Fields section as per <a href="http://rails.lighthouseapp.com/projects/16213/tickets/36-adding-an-example-for-using-distinct-to-ar-finders">this ticket</a>
-</p>
-</li>
-<li>
-<p>
-November 23 2008: Added documentation for <tt>find_by_last</tt> and <tt>find_by_bang!</tt>
-</p>
-</li>
-<li>
-<p>
-November 21 2008: Fixed all points specified in <a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16-activerecord-finders#ticket-16-13">this comment</a> and <a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16-activerecord-finders#ticket-16-14">this comment</a>
-</p>
-</li>
-<li>
-<p>
-November 18 2008: Fixed all points specified in <a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16-activerecord-finders#ticket-16-11">this comment</a>
-</p>
-</li>
-<li>
-<p>
-November 8, 2008: Editing pass by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> . First release version.
-</p>
-</li>
-<li>
-<p>
-October 27, 2008: Added scoped section, added named params for conditions and added sub-section headers for conditions section by Ryan Bigg
-</p>
-</li>
-<li>
-<p>
-October 27, 2008: Fixed up all points specified in <a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16-activerecord-finders#ticket-16-6">this comment</a> with an exception of the final point by Ryan Bigg
-</p>
-</li>
-<li>
-<p>
-October 26, 2008: Editing pass by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> . First release version.
-</p>
-</li>
-<li>
-<p>
-October 22, 2008: Calculations complete, first complete draft by Ryan Bigg
-</p>
-</li>
-<li>
-<p>
-October 21, 2008: Extended named scope section by Ryan Bigg
-</p>
-</li>
-<li>
-<p>
-October 9, 2008: Lock, count, cleanup by Ryan Bigg
-</p>
-</li>
-<li>
-<p>
-October 6, 2008: Eager loading by Ryan Bigg
-</p>
-</li>
-<li>
-<p>
-October 5, 2008: Covered conditions by Ryan Bigg
-</p>
-</li>
-<li>
-<p>
-October 1, 2008: Covered limit/offset, formatting changes by Ryan Bigg
-</p>
-</li>
-<li>
-<p>
-September 28, 2008: Covered first/last/all by Ryan Bigg
+December 29 2008: Initial version by Ryan Bigg
</p>
</li>
</ul></div>
diff --git a/railties/doc/guides/html/performance_testing.html b/railties/doc/guides/html/performance_testing.html index 2e6ba0a891..ddad6506a9 100644 --- a/railties/doc/guides/html/performance_testing.html +++ b/railties/doc/guides/html/performance_testing.html @@ -214,6 +214,8 @@ ul#navMain { <li><a href="#_tuning_test_runs">Tuning Test Runs</a></li> + <li><a href="#_performance_test_environment">Performance Test Environment</a></li> + <li><a href="#gc">Installing GC-Patched Ruby</a></li> </ul> @@ -560,12 +562,23 @@ http://www.gnu.org/software/src-highlite --> <td class="content">Performance test configurability is not yet enabled in Rails. But it will be soon.</td>
</tr></table>
</div>
-<h3 id="gc">1.7. Installing GC-Patched Ruby</h3>
+<h3 id="_performance_test_environment">1.7. Performance Test Environment</h3>
+<div class="paragraph"><p>Performance tests are run in the <tt>development</tt> environment. But running performance tests will set the following configuration parameters:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>perform_caching <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
+ActiveSupport<span style="color: #990000">::</span>Dependencies<span style="color: #990000">.</span>mechanism <span style="color: #990000">=</span> <span style="color: #990000">:</span>require
+Rails<span style="color: #990000">.</span>logger<span style="color: #990000">.</span>level <span style="color: #990000">=</span> ActiveSupport<span style="color: #990000">::</span>BufferedLogger<span style="color: #990000">::</span>INFO</tt></pre></div></div>
+<div class="paragraph"><p>As <tt>ActionController::Base.perform_caching</tt> is set to <tt>true</tt>, performance tests will behave much as they do in the production environment.</p></div>
+<h3 id="gc">1.8. Installing GC-Patched Ruby</h3>
<div class="paragraph"><p>To get the best from Rails performance tests, you need to build a special Ruby binary with some super powers - <a href="http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch">GC patch</a> for measuring GC Runs/Time and memory/object allocation.</p></div>
<div class="paragraph"><p>The process is fairly straight forward. If you’ve never compiled a Ruby binary before, follow these steps to build a ruby binary inside your home directory:</p></div>
-<h4 id="_installation">1.7.1. Installation</h4>
+<h4 id="_installation">1.8.1. Installation</h4>
<div class="paragraph"><p>Compile Ruby and apply this <a href="http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch">GC Patch</a>:</p></div>
-<h4 id="_download_and_extract">1.7.2. Download and Extract</h4>
+<h4 id="_download_and_extract">1.8.2. Download and Extract</h4>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -575,14 +588,14 @@ http://www.gnu.org/software/src-highlite --> <span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ wget <span style="color: #990000"><</span>download the latest stable ruby from ftp<span style="color: #990000">:</span>//ftp<span style="color: #990000">.</span>ruby-lang<span style="color: #990000">.</span>org/pub/ruby<span style="color: #990000">></span>
<span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ tar -xzvf <span style="color: #990000"><</span>ruby-version<span style="color: #990000">.</span>tar<span style="color: #990000">.</span>gz<span style="color: #990000">></span>
<span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ cd <span style="color: #990000"><</span>ruby-version<span style="color: #990000">></span></tt></pre></div></div>
-<h4 id="_apply_the_patch">1.7.3. Apply the patch</h4>
+<h4 id="_apply_the_patch">1.8.3. Apply the patch</h4>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="color: #990000">[</span>lifo@null ruby-version<span style="color: #990000">]</span>$ curl http<span style="color: #990000">:</span>//rubyforge<span style="color: #990000">.</span>org/tracker/download<span style="color: #990000">.</span>php<span style="color: #990000">/</span><span style="color: #993399">1814</span><span style="color: #990000">/</span><span style="color: #993399">7062</span><span style="color: #990000">/</span><span style="color: #993399">17676</span><span style="color: #990000">/</span><span style="color: #993399">3291</span>/ruby186gc<span style="color: #990000">.</span>patch <span style="color: #990000">|</span> patch -p<span style="color: #993399">0</span></tt></pre></div></div>
-<h4 id="_configure_and_install">1.7.4. Configure and Install</h4>
+<h4 id="_configure_and_install">1.8.4. Configure and Install</h4>
<div class="paragraph"><p>The following will install ruby in your home directory’s <tt>/rubygc</tt> directory. Make sure to replace <tt><homedir></tt> with a full patch to your actual home directory.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
@@ -591,7 +604,7 @@ http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="color: #990000">[</span>lifo@null ruby-version<span style="color: #990000">]</span>$ <span style="color: #990000">.</span>/configure --prefix<span style="color: #990000">=/<</span>homedir<span style="color: #990000">></span>/rubygc
<span style="color: #990000">[</span>lifo@null ruby-version<span style="color: #990000">]</span>$ make <span style="color: #990000">&&</span> make install</tt></pre></div></div>
-<h4 id="_prepare_aliases">1.7.5. Prepare aliases</h4>
+<h4 id="_prepare_aliases">1.8.5. Prepare aliases</h4>
<div class="paragraph"><p>For convenience, add the following lines in your <tt>~/.profile</tt>:</p></div>
<div class="listingblock">
<div class="content">
@@ -601,7 +614,7 @@ alias gcgem='~/rubygc/bin/gem' alias gcirb='~/rubygc/bin/irb'
alias gcrails='~/rubygc/bin/rails'</tt></pre>
</div></div>
-<h4 id="_install_rubygems_and_dependency_gems">1.7.6. Install rubygems and dependency gems</h4>
+<h4 id="_install_rubygems_and_dependency_gems">1.8.6. Install rubygems and dependency gems</h4>
<div class="paragraph"><p>Download <a href="http://rubyforge.org/projects/rubygems">Rubygems</a> and install it from source. Rubygem’s README file should have necessary installation instructions.</p></div>
<div class="paragraph"><p>Additionally, install the following gems :</p></div>
<div class="ulist"><ul>
diff --git a/railties/doc/guides/html/rails_on_rack.html b/railties/doc/guides/html/rails_on_rack.html index bf444e28e1..aeff78c02e 100644 --- a/railties/doc/guides/html/rails_on_rack.html +++ b/railties/doc/guides/html/rails_on_rack.html @@ -183,7 +183,7 @@ ul#navMain { <div id="logo"> <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a> </div> - + <h1 id="site_title"><span>Ruby on Rails</span></h1> <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2> @@ -194,7 +194,7 @@ ul#navMain { </div> <div id="container"> - + <div id="sidebar"> <h2>Chapters</h2> <ol> @@ -204,349 +204,388 @@ ul#navMain { <li> <a href="#_rails_on_rack">Rails on Rack</a> <ul> - + <li><a href="#_actioncontroller_dispatcher_new">ActionController::Dispatcher.new</a></li> - + <li><a href="#_script_server">script/server</a></li> - + <li><a href="#_rackup">rackup</a></li> - + </ul> </li> <li> <a href="#_action_controller_middleware_stack">Action Controller Middleware Stack</a> <ul> - + <li><a href="#_adding_middlewares">Adding Middlewares</a></li> - + <li><a href="#_internal_middleware_stack">Internal Middleware Stack</a></li> - - <li><a href="#_custom_internal_middleware_stack">Custom Internal Middleware Stack</a></li> - + + <li><a href="#_customizing_internal_middleware_stack">Customizing Internal Middleware Stack</a></li> + + <li><a href="#_inspecting_middleware_stack">Inspecting Middleware Stack</a></li> + </ul> </li> <li> <a href="#_rails_metal_applications">Rails Metal Applications</a> <ul> - + <li><a href="#_generating_a_metal_application">Generating a Metal Application</a></li> - + </ul> </li> <li> + <a href="#_middlewares_and_rails">Middlewares and Rails</a> + </li> + <li> <a href="#_changelog">Changelog</a> </li> </ol> </div> - + <div id="content"> <h1>Rails on Rack</h1> - <div id="preamble"> -<div class="sectionbody"> -<div class="paragraph"><p>This guide covers Rails integration with Rack and interfacing with other Rack components. By referring to this guide, you will be able to:</p></div> -<div class="ulist"><ul> -<li> -<p> -Create Rails Metal applications -</p> -</li> -<li> -<p> -Use Rack Middlewares in your Rails applications -</p> -</li> -<li> -<p> -Understand Action Pack’s internal Middleware stack -</p> -</li> -<li> -<p> -Define custom internal Middleware stack -</p> -</li> -<li> -<p> -Understand the best practices for developing a middleware aimed at Rails applications -</p> -</li> -</ul></div> -<div class="admonitionblock"> -<table><tr> -<td class="icon"> -<img src="./images/icons/note.png" alt="Note" /> -</td> -<td class="content">This guide assumes a working knowledge of Rack protocol and Rack concepts such as middlewares</td> -</tr></table> -</div> -</div> -</div> -<h2 id="_introduction_to_rack">1. Introduction to Rack</h2> -<div class="sectionbody"> -<div class="sidebarblock"> -<div class="sidebar-content"> -<div class="paragraph"><p>Rack provides a minimal, modular and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call.</p></div> -<div class="ulist"><ul> -<li> -<p> -<a href="http://rack.rubyforge.org/doc">Rack API Documentation</a> -</p> -</li> -</ul></div> -</div></div> -<div class="paragraph"><p>Explaining Rack is not really in the scope of this guide. In case you are not familiar with Rack’s basics, you should check out the following links:</p></div> -<div class="ulist"><ul> -<li> -<p> -<a href="http://rack.github.com">Official Rack Website</a> -</p> -</li> -<li> -<p> -<a href="http://chneukirchen.org/blog/archive/2007/02/introducing-rack.html">Introducing Rack</a> -</p> -</li> -<li> -<p> -<a href="http://m.onkey.org/2008/11/17/ruby-on-rack-1">Ruby on Rack #1 - Hello Rack!</a> -</p> -</li> -<li> -<p> -<a href="http://m.onkey.org/2008/11/18/ruby-on-rack-2-rack-builder">Ruby on Rack #2 - The Builder</a> -</p> -</li> -</ul></div> -</div> -<h2 id="_rails_on_rack">2. Rails on Rack</h2> -<div class="sectionbody"> -<h3 id="_actioncontroller_dispatcher_new">2.1. ActionController::Dispatcher.new</h3> -<div class="paragraph"><p><tt>ActionController::Dispatcher.new</tt> is the primary Rack application object of a Rails application. It responds to <tt>call</tt> method with a single <tt>env</tt> argument and returns a Rack response. Any Rack compliant web server should be using <tt>ActionController::Dispatcher.new</tt> object to serve a Rails application.</p></div> -<h3 id="_script_server">2.2. script/server</h3> -<div class="paragraph"><p><tt>script/server</tt> does the basic job of creating a <tt>Rack::Builder</tt> object and starting the webserver. This is Rails equivalent of Rack’s <tt>rackup</tt> script.</p></div> -<div class="paragraph"><p>Here’s how <tt>script/server</tt> creates an instance of <tt>Rack::Builder</tt></p></div> -<div class="listingblock"> -<div class="content"><!-- Generator: GNU source-highlight 2.9 -by Lorenzo Bettini -http://www.lorenzobettini.it -http://www.gnu.org/software/src-highlite --> -<pre><tt>app <span style="color: #990000">=</span> Rack<span style="color: #990000">::</span>Builder<span style="color: #990000">.</span>new <span style="color: #FF0000">{</span> - use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>LogTailer <span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> options<span style="color: #990000">[:</span>detach<span style="color: #990000">]</span> - use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>Static - use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>Debugger <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> options<span style="color: #990000">[:</span>debugger<span style="color: #990000">]</span> - run ActionController<span style="color: #990000">::</span>Dispatcher<span style="color: #990000">.</span>new -<span style="color: #FF0000">}</span><span style="color: #990000">.</span>to_app</tt></pre></div></div> -<div class="paragraph"><p>Middlewares used in the code above are most useful in development envrionment. The following table explains their usage:</p></div> -<div class="tableblock"> -<table rules="all" -width="100%" -frame="border" -cellspacing="0" cellpadding="4"> -<col width="50%" /> -<col width="50%" /> -<thead valign="top"> -<tr> -<th align="left">Middleware </th> -<th align="left">Purpose</th> -</tr> -</thead> -<tbody valign="top"> -<tr> -<td align="left"><p class="table">Rails::Rack::LogTailer</p></td> -<td align="left"><p class="table">Appends log file output to console</p></td> -</tr> -<tr> -<td align="left"><p class="table">Rails::Rack::Static</p></td> -<td align="left"><p class="table">Serves static files inside <tt>RAILS_ROOT/public</tt> directory</p></td> -</tr> -<tr> -<td align="left"><p class="table">Rails::Rack::Debugger</p></td> -<td align="left"><p class="table">Starts Debugger</p></td> -</tr> -</tbody> -</table> -</div> -<h3 id="_rackup">2.3. rackup</h3> -<div class="paragraph"><p>To use <tt>rackup</tt> instead of Rails' <tt>script/server</tt>, you can put the following inside <tt>config.ru</tt> of your Rails application’s root directory:</p></div> -<div class="listingblock"> -<div class="content"><!-- Generator: GNU source-highlight 2.9 -by Lorenzo Bettini -http://www.lorenzobettini.it -http://www.gnu.org/software/src-highlite --> -<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># RAILS_ROOT/config.ru</span></span> -<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">"config/environment"</span> - -use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>LogTailer -use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>Static -run ActionController<span style="color: #990000">::</span>Dispatcher<span style="color: #990000">.</span>new</tt></pre></div></div> -<div class="paragraph"><p>And start the server:</p></div> -<div class="listingblock"> -<div class="content"><!-- Generator: GNU source-highlight 2.9 -by Lorenzo Bettini -http://www.lorenzobettini.it -http://www.gnu.org/software/src-highlite --> -<pre><tt><span style="color: #990000">[</span>lifo@null application<span style="color: #990000">]</span>$ rackup</tt></pre></div></div> -<div class="paragraph"><p>To find out more about different <tt>rackup</tt> options:</p></div> -<div class="listingblock"> -<div class="content"><!-- Generator: GNU source-highlight 2.9 -by Lorenzo Bettini -http://www.lorenzobettini.it -http://www.gnu.org/software/src-highlite --> -<pre><tt><span style="color: #990000">[</span>lifo@null application<span style="color: #990000">]</span>$ rackup --help</tt></pre></div></div> -</div> -<h2 id="_action_controller_middleware_stack">3. Action Controller Middleware Stack</h2> -<div class="sectionbody"> -<div class="paragraph"><p>Many of Action Controller’s internal components are implemented as Rack middlewares. <tt>ActionController::Dispatcher</tt> uses <tt>ActionController::MiddlewareStack</tt> to combine various internal and external middlewares to form a complete Rails Rack application.</p></div> -<div class="admonitionblock"> -<table><tr> -<td class="icon"> -<img src="./images/icons/note.png" alt="Note" /> -</td> -<td class="content"> -<div class="title">What is ActionController::MiddlewareStack ?</div><tt>ActionController::MiddlewareStack</tt> is Rails equivalent of <tt>Rack::Builder</tt>, but built for better flexibility and more features to meet Rails' requirements.</td> -</tr></table> -</div> -<h3 id="_adding_middlewares">3.1. Adding Middlewares</h3> -<div class="paragraph"><p>Rails provides a very simple configuration interface for adding generic Rack middlewares to a Rails applications.</p></div> -<div class="paragraph"><p>Here’s how you can add middlewares via <tt>environment.rb</tt></p></div> -<div class="listingblock"> -<div class="content"><!-- Generator: GNU source-highlight 2.9 -by Lorenzo Bettini -http://www.lorenzobettini.it -http://www.gnu.org/software/src-highlite --> -<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># environment.rb</span></span> - -config<span style="color: #990000">.</span>middleware<span style="color: #990000">.</span>use Rack<span style="color: #990000">::</span>BounceFavicon</tt></pre></div></div> -<h3 id="_internal_middleware_stack">3.2. Internal Middleware Stack</h3> -<div class="listingblock"> -<div class="content"><!-- Generator: GNU source-highlight 2.9 -by Lorenzo Bettini -http://www.lorenzobettini.it -http://www.gnu.org/software/src-highlite --> -<pre><tt>use <span style="color: #FF0000">"ActionController::Lock"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=></span> lambda <span style="color: #FF0000">{</span> - <span style="color: #990000">!</span>ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>allow_concurrency -<span style="color: #FF0000">}</span> - -use <span style="color: #FF0000">"ActionController::Failsafe"</span> - -use <span style="color: #FF0000">"ActiveRecord::QueryCache"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=></span> lambda <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #0000FF">defined</span></span><span style="color: #990000">?(</span>ActiveRecord<span style="color: #990000">)</span> <span style="color: #FF0000">}</span> - -<span style="color: #990000">[</span><span style="color: #FF0000">"ActionController::Session::CookieStore"</span><span style="color: #990000">,</span> - <span style="color: #FF0000">"ActionController::Session::MemCacheStore"</span><span style="color: #990000">,</span> - <span style="color: #FF0000">"ActiveRecord::SessionStore"</span><span style="color: #990000">].</span>each <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>store<span style="color: #990000">|</span> - use<span style="color: #990000">(</span>store<span style="color: #990000">,</span> ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>session_options<span style="color: #990000">,</span> - <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=></span> lambda <span style="color: #FF0000">{</span> - <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> session_store <span style="color: #990000">=</span> ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>session_store - session_store<span style="color: #990000">.</span>name <span style="color: #990000">==</span> store - <span style="font-weight: bold"><span style="color: #0000FF">end</span></span> - <span style="color: #FF0000">}</span> - <span style="color: #990000">)</span> -<span style="font-weight: bold"><span style="color: #0000FF">end</span></span> - -use ActionController<span style="color: #990000">::</span>VerbPiggybacking</tt></pre></div></div> -<div class="tableblock"> -<table rules="all" -width="100%" -frame="border" -cellspacing="0" cellpadding="4"> -<col width="50%" /> -<col width="50%" /> -<thead valign="top"> -<tr> -<th align="left">Middleware </th> -<th align="left">Purpose</th> -</tr> -</thead> -<tbody valign="top"> -<tr> -<td align="left"><p class="table">ActionController::Lock</p></td> -<td align="left"><p class="table">Mutex</p></td> -</tr> -<tr> -<td align="left"><p class="table">ActionController::Failsafe</p></td> -<td align="left"><p class="table">Never fail</p></td> -</tr> -<tr> -<td align="left"><p class="table">ActiveRecord::QueryCache</p></td> -<td align="left"><p class="table">Query caching</p></td> -</tr> -<tr> -<td align="left"><p class="table">ActionController::Session::CookieStore</p></td> -<td align="left"><p class="table">Query caching</p></td> -</tr> -<tr> -<td align="left"><p class="table">ActionController::Session::MemCacheStore</p></td> -<td align="left"><p class="table">Query caching</p></td> -</tr> -<tr> -<td align="left"><p class="table">ActiveRecord::SessionStore</p></td> -<td align="left"><p class="table">Query caching</p></td> -</tr> -<tr> -<td align="left"><p class="table">ActionController::VerbPiggybacking</p></td> -<td align="left"><p class="table">_method hax</p></td> -</tr> -</tbody> -</table> -</div> -<h3 id="_custom_internal_middleware_stack">3.3. Custom Internal Middleware Stack</h3> -<div class="paragraph"><p>VERIFY THIS WORKS. Just a code dump at the moment.</p></div> -<div class="paragraph"><p>Put the following in an initializer.</p></div> -<div class="listingblock"> -<div class="content"><!-- Generator: GNU source-highlight 2.9 -by Lorenzo Bettini -http://www.lorenzobettini.it -http://www.gnu.org/software/src-highlite --> -<pre><tt>ActionController<span style="color: #990000">::</span>Dispatcher<span style="color: #990000">.</span>middleware <span style="color: #990000">=</span> ActionController<span style="color: #990000">::</span>MiddlewareStack<span style="color: #990000">.</span>new <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>m<span style="color: #990000">|</span> - m<span style="color: #990000">.</span>use ActionController<span style="color: #990000">::</span>Lock - m<span style="color: #990000">.</span>use ActionController<span style="color: #990000">::</span>Failsafe - m<span style="color: #990000">.</span>use ActiveRecord<span style="color: #990000">::</span>QueryCache - m<span style="color: #990000">.</span>use ActionController<span style="color: #990000">::</span>Session<span style="color: #990000">::</span>CookieStore - m<span style="color: #990000">.</span>use ActionController<span style="color: #990000">::</span>VerbPiggybacking -<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div> -</div> -<h2 id="_rails_metal_applications">4. Rails Metal Applications</h2> -<div class="sectionbody"> -<div class="paragraph"><p>Rails Metal applications are minimal Rack applications specially designed for integrating with a typical Rails applications. As Rails Metal Applications skip all of the Action Controller stack, serving a request has no overhead from the Rails framework itself. This is especially useful for infrequent cases where the performance of the full stack Rails framework is an issue.</p></div> -<h3 id="_generating_a_metal_application">4.1. Generating a Metal Application</h3> -<div class="paragraph"><p>Rails provides a generator called <tt>performance_test</tt> for creating new performance tests:</p></div> -<div class="listingblock"> -<div class="content"><!-- Generator: GNU source-highlight 2.9 -by Lorenzo Bettini -http://www.lorenzobettini.it -http://www.gnu.org/software/src-highlite --> -<pre><tt>script/generate metal poller</tt></pre></div></div> -<div class="paragraph"><p>This generates <tt>poller.rb</tt> in the <tt>app/metal</tt> directory:</p></div> -<div class="listingblock"> -<div class="content"><!-- Generator: GNU source-highlight 2.9 -by Lorenzo Bettini -http://www.lorenzobettini.it -http://www.gnu.org/software/src-highlite --> -<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># Allow the metal piece to run in isolation</span></span> -<span style="font-weight: bold"><span style="color: #000080">require</span></span><span style="color: #990000">(</span>File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #FF0000">"/../../config/environment"</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> <span style="font-weight: bold"><span style="color: #0000FF">defined</span></span><span style="color: #990000">?(</span>Rails<span style="color: #990000">)</span> - -<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Poller - <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>call<span style="color: #990000">(</span>env<span style="color: #990000">)</span> - <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> env<span style="color: #990000">[</span><span style="color: #FF0000">"PATH_INFO"</span><span style="color: #990000">]</span> <span style="color: #990000">=~</span> <span style="color: #FF6600">/^\/poller/</span> - <span style="color: #990000">[</span><span style="color: #993399">200</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">"Content-Type"</span> <span style="color: #990000">=></span> <span style="color: #FF0000">"text/html"</span><span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #990000">[</span><span style="color: #FF0000">"Hello, World!"</span><span style="color: #990000">]]</span> - <span style="font-weight: bold"><span style="color: #0000FF">else</span></span> - <span style="color: #990000">[</span><span style="color: #993399">404</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">"Content-Type"</span> <span style="color: #990000">=></span> <span style="color: #FF0000">"text/html"</span><span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #990000">[</span><span style="color: #FF0000">"Not Found"</span><span style="color: #990000">]]</span> - <span style="font-weight: bold"><span style="color: #0000FF">end</span></span> - <span style="font-weight: bold"><span style="color: #0000FF">end</span></span> -<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div> -<div class="paragraph"><p>Metal applications are an optimization. You should make sure to <a href="http://weblog.rubyonrails.org/2008/12/20/performance-of-rails-metal">understand the related performance implications</a> before using it.</p></div> -</div> -<h2 id="_changelog">5. Changelog</h2> -<div class="sectionbody"> -<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/4">Lighthouse ticket</a></p></div> -<div class="ulist"><ul> -<li> -<p> -January 11, 2009: First version by Pratik -</p> -</li> -</ul></div> -</div> + <div id="preamble">
+<div class="sectionbody">
+<div class="paragraph"><p>This guide covers Rails integration with Rack and interfacing with other Rack components. By referring to this guide, you will be able to:</p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+Create Rails Metal applications
+</p>
+</li>
+<li>
+<p>
+Use Rack Middlewares in your Rails applications
+</p>
+</li>
+<li>
+<p>
+Understand Action Pack’s internal Middleware stack
+</p>
+</li>
+<li>
+<p>
+Define custom internal Middleware stack
+</p>
+</li>
+<li>
+<p>
+Understand the best practices for developing a middleware aimed at Rails applications
+</p>
+</li>
+</ul></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/note.png" alt="Note" />
+</td>
+<td class="content">This guide assumes a working knowledge of Rack protocol and Rack concepts such as middlewares, url maps and Rack::Builder.</td>
+</tr></table>
+</div>
+</div>
+</div>
+<h2 id="_introduction_to_rack">1. Introduction to Rack</h2>
+<div class="sectionbody">
+<div class="sidebarblock">
+<div class="sidebar-content">
+<div class="paragraph"><p>Rack provides a minimal, modular and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call.</p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+<a href="http://rack.rubyforge.org/doc">Rack API Documentation</a>
+</p>
+</li>
+</ul></div>
+</div></div>
+<div class="paragraph"><p>Explaining Rack is not really in the scope of this guide. In case you are not familiar with Rack’s basics, you should check out the following links:</p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+<a href="http://rack.github.com">Official Rack Website</a>
+</p>
+</li>
+<li>
+<p>
+<a href="http://chneukirchen.org/blog/archive/2007/02/introducing-rack.html">Introducing Rack</a>
+</p>
+</li>
+<li>
+<p>
+<a href="http://m.onkey.org/2008/11/17/ruby-on-rack-1">Ruby on Rack #1 - Hello Rack!</a>
+</p>
+</li>
+<li>
+<p>
+<a href="http://m.onkey.org/2008/11/18/ruby-on-rack-2-rack-builder">Ruby on Rack #2 - The Builder</a>
+</p>
+</li>
+</ul></div>
+</div>
+<h2 id="_rails_on_rack">2. Rails on Rack</h2>
+<div class="sectionbody">
+<h3 id="_actioncontroller_dispatcher_new">2.1. ActionController::Dispatcher.new</h3>
+<div class="paragraph"><p><tt>ActionController::Dispatcher.new</tt> is the primary Rack application object of a Rails application. It responds to <tt>call</tt> method with a single <tt>env</tt> argument and returns a Rack response. Any Rack compliant web server should be using <tt>ActionController::Dispatcher.new</tt> object to serve a Rails application.</p></div>
+<h3 id="_script_server">2.2. script/server</h3>
+<div class="paragraph"><p><tt>script/server</tt> does the basic job of creating a <tt>Rack::Builder</tt> object and starting the webserver. This is Rails equivalent of Rack’s <tt>rackup</tt> script.</p></div>
+<div class="paragraph"><p>Here’s how <tt>script/server</tt> creates an instance of <tt>Rack::Builder</tt></p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>app <span style="color: #990000">=</span> Rack<span style="color: #990000">::</span>Builder<span style="color: #990000">.</span>new <span style="color: #FF0000">{</span>
+ use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>LogTailer <span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> options<span style="color: #990000">[:</span>detach<span style="color: #990000">]</span>
+ use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>Static
+ use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>Debugger <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> options<span style="color: #990000">[:</span>debugger<span style="color: #990000">]</span>
+ run ActionController<span style="color: #990000">::</span>Dispatcher<span style="color: #990000">.</span>new
+<span style="color: #FF0000">}</span><span style="color: #990000">.</span>to_app</tt></pre></div></div>
+<div class="paragraph"><p>Middlewares used in the code above are most useful in development envrionment. The following table explains their usage:</p></div>
+<div class="tableblock">
+<table rules="all"
+width="100%"
+frame="border"
+cellspacing="0" cellpadding="4">
+<col width="50%" />
+<col width="50%" />
+<thead valign="top">
+<tr>
+<th align="left">Middleware </th>
+<th align="left">Purpose</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr>
+<td align="left"><p class="table">Rails::Rack::LogTailer</p></td>
+<td align="left"><p class="table">Appends log file output to console</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">Rails::Rack::Static</p></td>
+<td align="left"><p class="table">Serves static files inside <tt>RAILS_ROOT/public</tt> directory</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">Rails::Rack::Debugger</p></td>
+<td align="left"><p class="table">Starts Debugger</p></td>
+</tr>
+</tbody>
+</table>
+</div>
+<h3 id="_rackup">2.3. rackup</h3>
+<div class="paragraph"><p>To use <tt>rackup</tt> instead of Rails' <tt>script/server</tt>, you can put the following inside <tt>config.ru</tt> of your Rails application’s root directory:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># RAILS_ROOT/config.ru</span></span>
+<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">"config/environment"</span>
+
+use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>LogTailer
+use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>Static
+run ActionController<span style="color: #990000">::</span>Dispatcher<span style="color: #990000">.</span>new</tt></pre></div></div>
+<div class="paragraph"><p>And start the server:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="color: #990000">[</span>lifo@null application<span style="color: #990000">]</span>$ rackup</tt></pre></div></div>
+<div class="paragraph"><p>To find out more about different <tt>rackup</tt> options:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="color: #990000">[</span>lifo@null application<span style="color: #990000">]</span>$ rackup --help</tt></pre></div></div>
+</div>
+<h2 id="_action_controller_middleware_stack">3. Action Controller Middleware Stack</h2>
+<div class="sectionbody">
+<div class="paragraph"><p>Many of Action Controller’s internal components are implemented as Rack middlewares. <tt>ActionController::Dispatcher</tt> uses <tt>ActionController::MiddlewareStack</tt> to combine various internal and external middlewares to form a complete Rails Rack application.</p></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/note.png" alt="Note" />
+</td>
+<td class="content">
+<div class="title">What is ActionController::MiddlewareStack ?</div><tt>ActionController::MiddlewareStack</tt> is Rails equivalent of <tt>Rack::Builder</tt>, but built for better flexibility and more features to meet Rails' requirements.</td>
+</tr></table>
+</div>
+<h3 id="_adding_middlewares">3.1. Adding Middlewares</h3>
+<div class="paragraph"><p>Rails provides a very simple configuration interface for adding generic Rack middlewares to a Rails applications.</p></div>
+<div class="paragraph"><p>Here’s how you can add middlewares via <tt>environment.rb</tt></p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># environment.rb</span></span>
+
+config<span style="color: #990000">.</span>middleware<span style="color: #990000">.</span>use Rack<span style="color: #990000">::</span>BounceFavicon</tt></pre></div></div>
+<h3 id="_internal_middleware_stack">3.2. Internal Middleware Stack</h3>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>use <span style="color: #FF0000">"ActionController::Lock"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=></span> lambda <span style="color: #FF0000">{</span>
+ <span style="color: #990000">!</span>ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>allow_concurrency
+<span style="color: #FF0000">}</span>
+
+use <span style="color: #FF0000">"ActionController::Failsafe"</span>
+
+use <span style="color: #FF0000">"ActiveRecord::QueryCache"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=></span> lambda <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #0000FF">defined</span></span><span style="color: #990000">?(</span>ActiveRecord<span style="color: #990000">)</span> <span style="color: #FF0000">}</span>
+
+<span style="color: #990000">[</span><span style="color: #FF0000">"ActionController::Session::CookieStore"</span><span style="color: #990000">,</span>
+ <span style="color: #FF0000">"ActionController::Session::MemCacheStore"</span><span style="color: #990000">,</span>
+ <span style="color: #FF0000">"ActiveRecord::SessionStore"</span><span style="color: #990000">].</span>each <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>store<span style="color: #990000">|</span>
+ use<span style="color: #990000">(</span>store<span style="color: #990000">,</span> ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>session_options<span style="color: #990000">,</span>
+ <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=></span> lambda <span style="color: #FF0000">{</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> session_store <span style="color: #990000">=</span> ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>session_store
+ session_store<span style="color: #990000">.</span>name <span style="color: #990000">==</span> store
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+ <span style="color: #FF0000">}</span>
+ <span style="color: #990000">)</span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+
+use ActionController<span style="color: #990000">::</span>VerbPiggybacking</tt></pre></div></div>
+<div class="tableblock">
+<table rules="all"
+width="100%"
+frame="border"
+cellspacing="0" cellpadding="4">
+<col width="50%" />
+<col width="50%" />
+<thead valign="top">
+<tr>
+<th align="left">Middleware </th>
+<th align="left">Purpose</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr>
+<td align="left"><p class="table">ActionController::Lock</p></td>
+<td align="left"><p class="table">Mutex</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">ActionController::Failsafe</p></td>
+<td align="left"><p class="table">Never fail</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">ActiveRecord::QueryCache</p></td>
+<td align="left"><p class="table">Query caching</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">ActionController::Session::CookieStore</p></td>
+<td align="left"><p class="table">Query caching</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">ActionController::Session::MemCacheStore</p></td>
+<td align="left"><p class="table">Query caching</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">ActiveRecord::SessionStore</p></td>
+<td align="left"><p class="table">Query caching</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">ActionController::VerbPiggybacking</p></td>
+<td align="left"><p class="table">_method hax</p></td>
+</tr>
+</tbody>
+</table>
+</div>
+<h3 id="_customizing_internal_middleware_stack">3.3. Customizing Internal Middleware Stack</h3>
+<div class="paragraph"><p>VERIFY THIS WORKS. Just a code dump at the moment.</p></div>
+<div class="paragraph"><p>Put the following in an initializer.</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>ActionController<span style="color: #990000">::</span>Dispatcher<span style="color: #990000">.</span>middleware <span style="color: #990000">=</span> ActionController<span style="color: #990000">::</span>MiddlewareStack<span style="color: #990000">.</span>new <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>m<span style="color: #990000">|</span>
+ m<span style="color: #990000">.</span>use ActionController<span style="color: #990000">::</span>Lock
+ m<span style="color: #990000">.</span>use ActionController<span style="color: #990000">::</span>Failsafe
+ m<span style="color: #990000">.</span>use ActiveRecord<span style="color: #990000">::</span>QueryCache
+ m<span style="color: #990000">.</span>use ActionController<span style="color: #990000">::</span>Session<span style="color: #990000">::</span>CookieStore
+ m<span style="color: #990000">.</span>use ActionController<span style="color: #990000">::</span>VerbPiggybacking
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Josh says :</p></div>
+<div class="sidebarblock">
+<div class="sidebar-content">
+<div class="paragraph"><p>3.3: I wouldn’t recommend this: custom internal stack
+i’d recommend using config.middleware.use api
+we still need a better api for swapping out existing middleware, etc
+config.middleware.swap AC::Sessions, My::Sessoins
+or something like that</p></div>
+</div></div>
+<h3 id="_inspecting_middleware_stack">3.4. Inspecting Middleware Stack</h3>
+<div class="paragraph"><p>Rails has a handy rake task for inspecting the middleware stack in use:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>$ rake middleware</tt></pre></div></div>
+<div class="paragraph"><p>For a freshly generated Rails application, this will produce:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>use ActionController<span style="color: #990000">::</span>Lock
+use ActionController<span style="color: #990000">::</span>Failsafe
+use ActiveRecord<span style="color: #990000">::</span>QueryCache
+use ActionController<span style="color: #990000">::</span>Session<span style="color: #990000">::</span>CookieStore<span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #990000">:</span>secret<span style="color: #990000">=></span><span style="color: #FF0000">"aa5150a22c1a5f24112260c33ae2131a88d7539117bfdcd5696fb2be385b60c6da9f7d4ed0a67e3b8cc85cc4e653ba0111dd1f3f8999520f049e2262068c16a6"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>session_key<span style="color: #990000">=></span><span style="color: #FF0000">"_edge_session"</span><span style="color: #FF0000">}</span>
+use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>Metal
+use ActionController<span style="color: #990000">::</span>VerbPiggybacking
+run ActionController<span style="color: #990000">::</span>Dispatcher<span style="color: #990000">.</span>new</tt></pre></div></div>
+<div class="paragraph"><p>This rake task is very useful if the application requires highly customized Rack middleware setup.</p></div>
+</div>
+<h2 id="_rails_metal_applications">4. Rails Metal Applications</h2>
+<div class="sectionbody">
+<div class="paragraph"><p>Rails Metal applications are minimal Rack applications specially designed for integrating with a typical Rails application. As Rails Metal Applications skip all of the Action Controller stack, serving a request has no overhead from the Rails framework itself. This is especially useful for infrequent cases where the performance of the full stack Rails framework is an issue.</p></div>
+<h3 id="_generating_a_metal_application">4.1. Generating a Metal Application</h3>
+<div class="paragraph"><p>Rails provides a generator called <tt>performance_test</tt> for creating new performance tests:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>script/generate metal poller</tt></pre></div></div>
+<div class="paragraph"><p>This generates <tt>poller.rb</tt> in the <tt>app/metal</tt> directory:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># Allow the metal piece to run in isolation</span></span>
+<span style="font-weight: bold"><span style="color: #000080">require</span></span><span style="color: #990000">(</span>File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #FF0000">"/../../config/environment"</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> <span style="font-weight: bold"><span style="color: #0000FF">defined</span></span><span style="color: #990000">?(</span>Rails<span style="color: #990000">)</span>
+
+<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Poller
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>call<span style="color: #990000">(</span>env<span style="color: #990000">)</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> env<span style="color: #990000">[</span><span style="color: #FF0000">"PATH_INFO"</span><span style="color: #990000">]</span> <span style="color: #990000">=~</span> <span style="color: #FF6600">/^\/poller/</span>
+ <span style="color: #990000">[</span><span style="color: #993399">200</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">"Content-Type"</span> <span style="color: #990000">=></span> <span style="color: #FF0000">"text/html"</span><span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #990000">[</span><span style="color: #FF0000">"Hello, World!"</span><span style="color: #990000">]]</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
+ <span style="color: #990000">[</span><span style="color: #993399">404</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">"Content-Type"</span> <span style="color: #990000">=></span> <span style="color: #FF0000">"text/html"</span><span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #990000">[</span><span style="color: #FF0000">"Not Found"</span><span style="color: #990000">]]</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Metal applications are an optimization. You should make sure to <a href="http://weblog.rubyonrails.org/2008/12/20/performance-of-rails-metal">understand the related performance implications</a> before using it.</p></div>
+</div>
+<h2 id="_middlewares_and_rails">5. Middlewares and Rails</h2>
+<div class="sectionbody">
+</div>
+<h2 id="_changelog">6. Changelog</h2>
+<div class="sectionbody">
+<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/4">Lighthouse ticket</a></p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+January 11, 2009: First version by Pratik
+</p>
+</li>
+</ul></div>
+</div>
</div> </div> diff --git a/railties/doc/guides/html/testing_rails_applications.html b/railties/doc/guides/html/testing_rails_applications.html index ac6ecc73b9..457f93f622 100644 --- a/railties/doc/guides/html/testing_rails_applications.html +++ b/railties/doc/guides/html/testing_rails_applications.html @@ -1157,7 +1157,7 @@ by Lorenzo Bettini http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>assert_select <span style="color: #FF0000">'title'</span><span style="color: #990000">,</span> <span style="color: #FF0000">"Welcome to Rails Testing Guide"</span></tt></pre></div></div>
-<div class="paragraph"><p>You can also use nested <tt>assert_select</tt> blocks. In this case the inner <tt>assert_select</tt> will run the assertion on each element selected by the outer <tt>assert_select</tt> block:</p></div>
+<div class="paragraph"><p>You can also use nested <tt>assert_select</tt> blocks. In this case the inner <tt>assert_select</tt> runs the assertion on the complete collection of elements selected by the outer <tt>assert_select</tt> block:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1166,7 +1166,22 @@ http://www.gnu.org/software/src-highlite --> <pre><tt>assert_select <span style="color: #FF0000">'ul.navigation'</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
assert_select <span style="color: #FF0000">'li.menu_item'</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
-<div class="paragraph"><p>The <tt>assert_select</tt> assertion is quite powerful. For more advanced usage, refer to its <a href="http://api.rubyonrails.com/classes/ActionController/Assertions/SelectorAssertions.html#M000749">documentation</a>.</p></div>
+<div class="paragraph"><p>Alternatively the collection of elements selected by the outer <tt>assert_select</tt> may be iterated through so that <tt>assert_select</tt> may be called separately for each element. Suppose for example that the response contains two ordered lists, each with four list elements then the following tests will both pass.</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>assert_select <span style="color: #FF0000">"ol"</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>elements<span style="color: #990000">|</span>
+ elements<span style="color: #990000">.</span>each <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>element<span style="color: #990000">|</span>
+ assert_select element<span style="color: #990000">,</span> <span style="color: #FF0000">"li"</span><span style="color: #990000">,</span> <span style="color: #993399">4</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+
+assert_select <span style="color: #FF0000">"ol"</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
+ assert_select <span style="color: #FF0000">"li"</span><span style="color: #990000">,</span> <span style="color: #993399">8</span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>assert_select</tt> assertion is quite powerful. For more advanced usage, refer to its <a href="http://api.rubyonrails.com/classes/ActionController/Assertions/SelectorAssertions.html">documentation</a>.</p></div>
<h4 id="_additional_view_based_assertions">4.6.1. Additional View-based Assertions</h4>
<div class="paragraph"><p>There are more assertions that are primarily used in testing views:</p></div>
<div class="tableblock">
|