diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2008-11-02 04:02:40 +0530 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2008-11-02 04:02:40 +0530 |
commit | b047929c14f088d535eea460ddd8769f43cd4ae5 (patch) | |
tree | 221d816ef0c908044fd6029950ccad064866ab8f /railties | |
parent | 5a02f0bccf55191c2cfbcc69bd8165df6d7a2012 (diff) | |
download | rails-b047929c14f088d535eea460ddd8769f43cd4ae5.tar.gz rails-b047929c14f088d535eea460ddd8769f43cd4ae5.tar.bz2 rails-b047929c14f088d535eea460ddd8769f43cd4ae5.zip |
Merge with docrails
Diffstat (limited to 'railties')
19 files changed, 936 insertions, 596 deletions
diff --git a/railties/doc/guides/html/2_2_release_notes.html b/railties/doc/guides/html/2_2_release_notes.html index 7e975d35a6..c68f10ad5a 100644 --- a/railties/doc/guides/html/2_2_release_notes.html +++ b/railties/doc/guides/html/2_2_release_notes.html @@ -337,72 +337,72 @@ More information : <div class="ilist"><ul>
<li>
<p>
-Getting Started with Rails
+<a href="http://guides.rubyonrails.org/getting_started_with_rails.html">Getting Started with Rails</a>
</p>
</li>
<li>
<p>
-Rails Database Migrations
+<a href="http://guides.rubyonrails.org/migrations.html">Rails Database Migrations</a>
</p>
</li>
<li>
<p>
-Active Record Associations
+<a href="http://guides.rubyonrails.org/association_basics.html">Active Record Associations</a>
</p>
</li>
<li>
<p>
-Active Record Finders
+<a href="http://guides.rubyonrails.org/finders.html">Active Record Finders</a>
</p>
</li>
<li>
<p>
-Layouts and Rendering in Rails
+<a href="http://guides.rubyonrails.org/layouts_and_rendering.html">Layouts and Rendering in Rails</a>
</p>
</li>
<li>
<p>
-Action View Form Helpers
+<a href="http://guides.rubyonrails.org/form_helpers.html">Action View Form Helpers</a>
</p>
</li>
<li>
<p>
-Rails Routing from the Outside In
+<a href="http://guides.rubyonrails.org/routing_outside_in.html">Rails Routing from the Outside In</a>
</p>
</li>
<li>
<p>
-Basics of Action Controller
+<a href="http://guides.rubyonrails.org/actioncontroller_basics.html">Basics of Action Controller</a>
</p>
</li>
<li>
<p>
-Rails Caching
+<a href="http://guides.rubyonrails.org/caching_with_rails.html">Rails Caching</a>
</p>
</li>
<li>
<p>
-Testing Rails Applications
+<a href="http://guides.rubyonrails.org/testing_rails_applications.html">Testing Rails Applications</a>
</p>
</li>
<li>
<p>
-Securing Rails Applications
+<a href="http://guides.rubyonrails.org/security.html">Securing Rails Applications</a>
</p>
</li>
<li>
<p>
-Debugging Rails Applications
+<a href="http://guides.rubyonrails.org/debugging_rails_applications.html">Debugging Rails Applications</a>
</p>
</li>
<li>
<p>
-Benchmarking and Profiling Rails Applications
+<a href="http://guides.rubyonrails.org/benchmarking_and_profiling.html">Benchmarking and Profiling Rails Applications</a>
</p>
</li>
<li>
<p>
-The Basics of Creating Rails Plugins
+<a href="http://guides.rubyonrails.org/creating_plugins.html">The Basics of Creating Rails Plugins</a>
</p>
</li>
</ul></div>
@@ -612,7 +612,7 @@ More information: </ul></div>
<h3 id="_new_dynamic_finders">5.4. New Dynamic Finders</h3>
<div class="para"><p>Two new sets of methods have been added to Active Record's dynamic finders family.</p></div>
-<h4 id="_find_last_by_lt_attributes_gt">5.4.1. find_last_by_<attributes></h4>
+<h4 id="_tt_find_last_by_lt_attribute_gt_tt">5.4.1. <tt>find_last_by_<attribute></tt></h4>
<div class="para"><p>The <tt>find_last_by_<attribute></tt> method is equivalent to <tt>Model.last(:conditions ⇒ {:attribute ⇒ value})</tt></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
@@ -629,8 +629,8 @@ Lead Contributor: <a href="http://www.workingwithrails.com/person/9147-emilio-ta </p>
</li>
</ul></div>
-<h4 id="_find_by_lt_attributes_gt">5.4.2. find_by_<attributes>!</h4>
-<div class="para"><p>The new bang! version of <tt>find_by_<attribute>! is equivalent to +Model.first(:conditions ⇒ {:attribute ⇒ value}) || raise ActiveRecord::RecordNotFound</tt> Instead of returning <tt>nil</tt> if it can't find a matching record, this method will raise an exception if it cannot find a match.</p></div>
+<h4 id="_tt_find_by_lt_attribute_gt_tt">5.4.2. <tt>find_by_<attribute>!</tt></h4>
+<div class="para"><p>The new bang! version of <tt>find_by_<attribute>!</tt> is equivalent to <tt>Model.first(:conditions ⇒ {:attribute ⇒ value}) || raise ActiveRecord::RecordNotFound</tt> Instead of returning <tt>nil</tt> if it can't find a matching record, this method will raise an exception if it cannot find a match.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -779,6 +779,16 @@ Benchmarking numbers are now reported in milliseconds rather than tiny fractions Rails now supports HTTP-only cookies (and uses them for sessions), which help mitigate some cross-site scripting risks in newer browsers.
</p>
</li>
+<li>
+<p>
+<tt>redirect_to</tt> now fully supports URI schemes (so, for example, you can redirect to a svn+ssh: URI).
+</p>
+</li>
+<li>
+<p>
+<tt>render</tt> now supports a <tt>:js</tt> option to render plain vanilla javascript with the right mime type.
+</p>
+</li>
</ul></div>
</div>
<h2 id="_action_view">7. Action View</h2>
@@ -791,7 +801,7 @@ Rails now supports HTTP-only cookies (and uses them for sessions), which help mi </li>
<li>
<p>
-The included Prototype javascript library has been upgraded to version 1.6.0.2.
+The included Prototype javascript library has been upgraded to version 1.6.0.3.
</p>
</li>
<li>
@@ -893,23 +903,23 @@ http://www.gnu.org/software/src-highlite --> 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> Vendor <span style="color: #990000"><<</span> ActiveRecord<span style="color: #990000">::</span>Base
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Vendor <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
has_one <span style="color: #990000">:</span>account
delegate <span style="color: #990000">:</span>email<span style="color: #990000">,</span> <span style="color: #990000">:</span>password<span style="color: #990000">,</span> <span style="color: #990000">:</span>to <span style="color: #990000">=></span> <span style="color: #990000">:</span>account<span style="color: #990000">,</span> <span style="color: #990000">:</span>prefix <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>
</tt></pre></div></div>
-<div class="para"><p>This will produce delegated methods <tt>vendor.account_email</tt> and <tt>vendor.account_password</tt>. You can also specify a custom prefix:</p></div>
+<div class="para"><p>This will produce delegated methods <tt>vendor#account_email</tt> and <tt>vendor#account_password</tt>. You can also specify a custom prefix:</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-weight: bold"><span style="color: #0000FF">class</span></span> Vendor <span style="color: #990000"><<</span> ActiveRecord<span style="color: #990000">::</span>Base
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Vendor <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
has_one <span style="color: #990000">:</span>account
delegate <span style="color: #990000">:</span>email<span style="color: #990000">,</span> <span style="color: #990000">:</span>password<span style="color: #990000">,</span> <span style="color: #990000">:</span>to <span style="color: #990000">=></span> <span style="color: #990000">:</span>account<span style="color: #990000">,</span> <span style="color: #990000">:</span>prefix <span style="color: #990000">=></span> <span style="color: #990000">:</span>owner
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
</tt></pre></div></div>
-<div class="para"><p>This will produce delegated methods <tt>vendor.owner_email</tt> and <tt>vendor.owner_password</tt>.</p></div>
+<div class="para"><p>This will produce delegated methods <tt>vendor#owner_email</tt> and <tt>vendor#owner_password</tt>.</p></div>
<div class="para"><p>Lead Contributor: <a href="http://workingwithrails.com/person/5830-daniel-schierbeck">Daniel Schierbeck</a></p></div>
<h3 id="_other_active_support_changes">9.4. Other Active Support Changes</h3>
<div class="ilist"><ul>
@@ -953,6 +963,11 @@ The addition of <tt>ActiveSupport::Rescuable</tt> allows any class to mix in the The included TzInfo library has been upgraded to version 0.3.11.
</p>
</li>
+<li>
+<p>
+<tt>ActiveSuport::StringInquirer</tt> gives you a pretty way to test for equality in strings: <tt>ActiveSupport::StringInquirer.new("abc").abc? ⇒ true</tt>
+</p>
+</li>
</ul></div>
</div>
<h2 id="_railties">10. Railties</h2>
@@ -997,7 +1012,7 @@ The included TzInfo library has been upgraded to version 0.3.11. </p>
</li>
</ul></div>
-<div class="para"><p>You can unpack or install a single gem by specifying <tt>GEM=_gem_name</tt> on the command line.</p></div>
+<div class="para"><p>You can unpack or install a single gem by specifying <tt>GEM=<em>gem_name</em></tt> on the command line.</p></div>
<div class="ilist"><ul>
<li>
<p>
@@ -1014,6 +1029,11 @@ More information: <a href="http://ryandaigle.com/articles/2008/4/1/what-s-new-in-edge-rails-gem-dependencies">What's New in Edge Rails: Gem Dependencies</a>
</p>
</li>
+<li>
+<p>
+<a href="http://afreshcup.com/2008/10/25/rails-212-and-22rc1-update-your-rubygems/">Rails 2.1.2 and 2.2RC1: Update Your RubyGems</a>
+</p>
+</li>
</ul></div>
</li>
</ul></div>
@@ -1046,7 +1066,7 @@ Instructions for setting up a continuous integration server to build Rails itsel </li>
<li>
<p>
-Wrapped <tt>Rails.env</tt> in <tt>StringQuestioneer</tt> so you can do <tt>Rails.env.development?</tt>
+Wrapped <tt>Rails.env</tt> in <tt>StringInquirer</tt> so you can do <tt>Rails.env.development?</tt>
</p>
</li>
<li>
diff --git a/railties/doc/guides/html/actioncontroller_basics.html b/railties/doc/guides/html/actioncontroller_basics.html index 2809034e27..32ddbe1f60 100644 --- a/railties/doc/guides/html/actioncontroller_basics.html +++ b/railties/doc/guides/html/actioncontroller_basics.html @@ -693,7 +693,6 @@ http://www.gnu.org/software/src-highlite --> <div class="para"><p>Now, the LoginsController's "new" and "create" actions will work as before without requiring the user to be logged in. The <tt>:only</tt> option is used to only skip this filter for these actions, and there is also an <tt>:except</tt> option which works the other way. These options can be used when adding filters too, so you can add a filter which only runs for selected actions in the first place.</p></div>
<h3 id="_after_filters_and_around_filters">6.1. After filters and around filters</h3>
<div class="para"><p>In addition to the before filters, you can run filters after an action has run or both before and after. The after filter is similar to the before filter, but because the action has already been run it has access to the response data that's about to be sent to the client. Obviously, after filters can not stop the action from running. Around filters are responsible for running the action, but they can choose not to, which is the around filter's way of stopping it.</p></div>
-<div class="para"><p>TODO: Find a real example for an around filter</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
diff --git a/railties/doc/guides/html/association_basics.html b/railties/doc/guides/html/association_basics.html index 012a1a0156..9159eaab2a 100644 --- a/railties/doc/guides/html/association_basics.html +++ b/railties/doc/guides/html/association_basics.html @@ -691,7 +691,7 @@ http://www.gnu.org/software/src-highlite --> belongs_to <span style="color: #990000">:</span>manager<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=></span> <span style="color: #FF0000">"User"</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
</tt></pre></div></div>
-<div class="para"><p>With this setup, you can retrieve <tt>@employee.subordinates</tt> and <tt>@employee.managers</tt>.</p></div>
+<div class="para"><p>With this setup, you can retrieve <tt>@employee.subordinates</tt> and <tt>@employee.manager</tt>.</p></div>
</div>
<h2 id="_tips_tricks_and_warnings">3. Tips, Tricks, and Warnings</h2>
<div class="sectionbody">
@@ -1185,6 +1185,14 @@ http://www.gnu.org/software/src-highlite --> <div class="para"><p>If you set the <tt>:readonly</tt> option to <tt>true</tt>, then the associated object will be read-only when retrieved via the association.</p></div>
<h5 id="_tt_select_tt"><tt>:select</tt></h5>
<div class="para"><p>The <tt>:select</tt> option lets you override the SQL <tt>SELECT</tt> clause that is used to retrieve data about the associated object. By default, Rails retrieves all columns.</p></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/tip.png" alt="Tip" />
+</td>
+<td class="content">If you set the <tt>:select</tt> option on a <tt>belongs_to</tt> association, you should also set the <tt>foreign_key</tt> option to guarantee the correct results.</td>
+</tr></table>
+</div>
<h5 id="_tt_validate_tt"><tt>:validate</tt></h5>
<div class="para"><p>If you set the <tt>:validate</tt> option to <tt>true</tt>, then associated objects will be validated whenever you save this object. By default, this is <tt>false</tt>: associated objects will not be validated when this object is saved.</p></div>
<h4 id="_when_are_objects_saved">4.1.3. When are Objects Saved?</h4>
diff --git a/railties/doc/guides/html/finders.html b/railties/doc/guides/html/finders.html index b10d390105..f8396bb517 100644 --- a/railties/doc/guides/html/finders.html +++ b/railties/doc/guides/html/finders.html @@ -202,7 +202,7 @@ ul#navMain { <a href="#_in_the_beginning_8230">In the beginning…</a> </li> <li> - <a href="#_our_models">Our Models</a> + <a href="#_the_sample_models">The Sample Models</a> </li> <li> <a href="#_database_agnostic">Database Agnostic</a> @@ -212,6 +212,15 @@ ul#navMain { </li> <li> <a href="#_conditions">Conditions</a> + <ul> + + <li><a href="#_pure_string_conditions">Pure String Conditions</a></li> + + <li><a href="#_array_conditions">Array Conditions</a></li> + + <li><a href="#_hash_conditions">Hash Conditions</a></li> + + </ul> </li> <li> <a href="#_ordering">Ordering</a> @@ -244,13 +253,16 @@ ul#navMain { <a href="#_finding_by_sql">Finding By SQL</a> </li> <li> + <a href="#_tt_select_all_tt"><tt>select_all</tt></a> + </li> + <li> <a href="#_working_with_associations">Working with Associations</a> </li> <li> <a href="#_named_scopes">Named Scopes</a> </li> <li> - <a href="#_existance_of_objects">Existance of Objects</a> + <a href="#_existence_of_objects">Existence of Objects</a> </li> <li> <a href="#_calculations">Calculations</a> @@ -272,24 +284,7 @@ ul#navMain { <a href="#_credits">Credits</a> </li> <li> - <a href="#_change_log">Change Log</a> - <ul> - - <li><a href="#_sunday_28_september_2008">Sunday, 28 September 2008</a></li> - - <li><a href="#_wednesday_01_october_2008">Wednesday, 01 October 2008</a></li> - - <li><a href="#_sunday_05_october_2008">Sunday, 05 October 2008</a></li> - - <li><a href="#_monday_06_october_2008">Monday, 06 October 2008</a></li> - - <li><a href="#_thursday_09_october_2008">Thursday, 09 October 2008</a></li> - - <li><a href="#_tuesday_21_october_2008">Tuesday, 21 October 2008</a></li> - - <li><a href="#_wednesday_22_october_2008">Wednesday, 22 October 2008</a></li> - - </ul> + <a href="#_changelog">Changelog</a> </li> </ol> </div> @@ -298,14 +293,14 @@ ul#navMain { <h1>Rails Finders</h1> <div id="preamble">
<div class="sectionbody">
-<div class="para"><p>This guide is all about the <tt>find</tt> method defined in ActiveRecord::Base, finding on associations, and associated goodness such as named scopes. You will learn how to be a find master.</p></div>
+<div class="para"><p>This guide is all about the <tt>find</tt> method defined in <tt>ActiveRecord::Base</tt>, finding on associations, and associated goodness such as named scopes. You will learn how to be a find master.</p></div>
</div>
</div>
<h2 id="_in_the_beginning_8230">1. In the beginning…</h2>
<div class="sectionbody">
<div class="para"><p>In the beginning there was SQL. SQL looked like this:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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 -->
@@ -314,13 +309,13 @@ http://www.gnu.org/software/src-highlite --> <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">0</span><span style="color: #990000">,</span><span style="color: #993399">1</span>
<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">ORDER</span></span> <span style="font-weight: bold"><span style="color: #0000FF">BY</span></span> id <span style="font-weight: bold"><span style="color: #0000FF">DESC</span></span> <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">0</span><span style="color: #990000">,</span><span style="color: #993399">1</span>
</tt></pre></div></div>
-<div class="para"><p>In Rails you don't usually have to type SQL (unlike other languages) because ActiveRecord is there to help you find your records.</p></div>
+<div class="para"><p>In Rails (unlike some other frameworks) you don't usually have to type SQL because Active Record is there to help you find your records.</p></div>
</div>
-<h2 id="_our_models">2. Our Models</h2>
+<h2 id="_the_sample_models">2. The Sample Models</h2>
<div class="sectionbody">
-<div class="para"><p>For this guide we have the following models:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<div class="para"><p>This guide demonstrates finding using the following models:</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 -->
@@ -330,82 +325,78 @@ http://www.gnu.org/software/src-highlite --> 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>
-</tt></pre></div></div>
-<div class="exampleblock">
-<div class="exampleblock-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
+
+<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>
-</tt></pre></div></div>
-<div class="exampleblock">
-<div class="exampleblock-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">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="exampleblock">
-<div class="exampleblock-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
+
+<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>
-</tt></pre></div></div>
-<div class="exampleblock">
-<div class="exampleblock-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
+
+<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">3. Database Agnostic</h2>
<div class="sectionbody">
-<div class="para"><p>ActiveRecord 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 ActiveRecord method format will always be the same.</p></div>
+<div class="para"><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>
<h2 id="_ids_first_last_and_all">4. IDs, First, Last and All</h2>
<div class="sectionbody">
-<div class="para"><p>ActiveRecord::Base has methods defined on it to make interacting with your database and the tables within it much, much easier: find. 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 Client.find(1) which would execute this query on your database:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<div class="para"><p><tt>ActiveRecord::Base</tt> 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">
+<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">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">`id`</span> <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">)</span>
-NOTE<span style="color: #990000">:</span> Please be aware that because this <span style="font-weight: bold"><span style="color: #0000FF">is</span></span> a standard <span style="font-weight: bold"><span style="color: #0000FF">table</span></span> created <span style="font-weight: bold"><span style="color: #0000FF">from</span></span> a migration <span style="font-weight: bold"><span style="color: #0000FF">in</span></span> Rails that the <span style="font-weight: bold"><span style="color: #0000FF">primary</span></span> <span style="font-weight: bold"><span style="color: #0000FF">key</span></span> <span style="font-weight: bold"><span style="color: #0000FF">is</span></span> defaulted <span style="font-weight: bold"><span style="color: #0000FF">to</span></span> <span style="color: #FF0000">'id'</span><span style="color: #990000">.</span> <span style="font-weight: bold"><span style="color: #0000FF">If</span></span> you have specified a different <span style="font-weight: bold"><span style="color: #0000FF">primary</span></span> <span style="font-weight: bold"><span style="color: #0000FF">key</span></span> <span style="font-weight: bold"><span style="color: #0000FF">in</span></span> your migrations<span style="color: #990000">,</span> this <span style="font-weight: bold"><span style="color: #0000FF">is</span></span> what Rails will find <span style="font-weight: bold"><span style="color: #0000FF">on</span></span> when you call the find method<span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">not</span></span> the id <span style="font-weight: bold"><span style="color: #0000FF">column</span></span><span style="color: #990000">.</span>
+<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: #990000">+</span>clients<span style="color: #990000">+</span> <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(+</span>clients<span style="color: #990000">+.+</span>id<span style="color: #990000">+</span> <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">)</span>
</tt></pre></div></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/note.png" alt="Note" />
+</td>
+<td class="content">Because this is a standard table created from a migration in Rail, the primary key is defaulted to <em>id</em>. If you have specified a different primary key in your migrations, this is what Rails will find on when you call the find method, not the id column.</td>
+</tr></table>
+</div>
<div class="para"><p>If you wanted to find clients with id 1 or 2, you call <tt>Client.find([1,2])</tt> or <tt>Client.find(1,2)</tt> and then this will be executed as:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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">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">`id`</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: #990000">[</span>source<span style="color: #990000">,</span>txt<span style="color: #990000">]</span>
-<span style="color: #990000">>></span> Client<span style="color: #990000">.</span>find<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: #990000">=></span> <span style="color: #990000">[</span><span style="font-style: italic"><span style="color: #9A1900">#<Client id: 1, name: => "Ryan", locked: false, orders_count: 2, created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50">, #<Client id: 2, name: => "Michael", locked: false, orders_count: 3, created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40">]</span></span>
+<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: #990000">+</span>clients<span style="color: #990000">+</span> <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(+</span>clients<span style="color: #990000">+.+</span>id<span style="color: #990000">+</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>
</tt></pre></div></div>
-<div class="para"><p>Note that if you pass in a list of numbers that the result will be returned as an array, not an object of Client.</p></div>
-<div class="para"><p>If you wanted to find the first client you would simply type <tt>Client.find(:first)</tt> and that would find the first client created in your clients table:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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>>> Client.find(:first)
-=> #<Client id: 1, name: => "Ryan", locked: false, orders_count: 2, created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50">
-If you were running script/server you may see the following output:
+<pre><tt>>> Client.find(1,2)
+=> [#<Client id: 1, name: => "Ryan", locked: false, orders_count: 2,
+ created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50">,
+ #<Client id: 2, name: => "Michael", locked: false, orders_count: 3,
+ created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40">]
</tt></pre></div></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<div class="para"><p>Note that if you pass in a list of numbers that the result will be returned as an array, not as a single <tt>Client</tt> object.</p></div>
+<div class="para"><p>If you wanted to find the first client you would simply type <tt>Client.first</tt> and that would find the first client created in your clients table:</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>>> Client.first
+=> #<Client id: 1, name: => "Ryan", locked: false, orders_count: 2,
+ created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50">
+</tt></pre></div></div>
+<div class="para"><p>If you were running script/server you might see the following output:</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 -->
@@ -413,142 +404,186 @@ http://www.gnu.org/software/src-highlite --> </tt></pre></div></div>
<div class="para"><p>Indicating the query that Rails has performed on your database.</p></div>
<div class="para"><p>To find the last client you would simply type <tt>Client.find(:last)</tt> and that would find the last client created in your clients table:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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>>> Client.find(:last)
-=> #<Client id: 2, name: => "Michael", locked: false, orders_count: 3, created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40">
+=> #<Client id: 2, name: => "Michael", locked: false, orders_count: 3,
+ created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40">
</tt></pre></div></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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">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">ORDER</span></span> <span style="font-weight: bold"><span style="color: #0000FF">BY</span></span> clients<span style="color: #990000">.</span>id <span style="font-weight: bold"><span style="color: #0000FF">DESC</span></span> <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">1</span>
</tt></pre></div></div>
-<div class="para"><p>To find all the clients you would simply type <tt>Client.find(:all)</tt> and that would find all the clients in your clients table:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<div class="para"><p>To find all the clients you would simply type <tt>Client.all</tt> and that would find all the clients in your clients table:</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>>> Client.find(:all)
-=> [#<Client id: 1, name: => "Ryan", locked: false, orders_count: 2, created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50">, #<Client id: 2, name: => "Michael", locked: false, orders_count: 3, created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40">]
+<pre><tt>>> Client.all
+=> [#<Client id: 1, name: => "Ryan", locked: false, orders_count: 2,
+ created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50">,
+ #<Client id: 2, name: => "Michael", locked: false, orders_count: 3,
+ created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40">]
</tt></pre></div></div>
-<div class="para"><p>Alternatively to calling Client.find(:first)/<tt>Client.find(:last)</tt>/<tt>Client.find(:all)</tt>, you could use the class method of <tt>Client.first</tt>/<tt>Client.last</tt>/<tt>Client.all</tt> instead. <tt>Client.first</tt>, <tt>Client.last</tt> and <tt>Client.all</tt> just call their longer counterparts.</p></div>
+<div class="para"><p>As alternatives to calling <tt>Client.first</tt>, <tt>Client.last</tt>, and <tt>Client.all</tt>, you can use the class methods <tt>Client.first</tt>, <tt>Client.last</tt>, and <tt>Client.all</tt> instead. <tt>Client.first</tt>, <tt>Client.last</tt> and <tt>Client.all</tt> just call their longer counterparts: <tt>Client.find(:first)</tt>, <tt>Client.find(:last)</tt> and <tt>Client.find(:all)</tt> respectively.</p></div>
<div class="para"><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 find will do also.</p></div>
</div>
<h2 id="_conditions">5. Conditions</h2>
<div class="sectionbody">
-<div class="para"><p>If you'd like to add conditions to your find, you could just specify them in there, just like <tt>Client.find(:first, :conditions ⇒ "orders_count = <em>2</em>")</tt>. Now what if that number could vary, say as a parameter from somewhere, or perhaps from the user's level status somewhere? The find then becomes something like <tt>Client.find(:first, :conditions ⇒ ["orders_count = ?", params[:orders]])</tt>. ActiveRecord 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.find(:first, :conditions ⇒ ["orders_count = ? AND locked = ?", params[:orders], false])</tt>. In this example, the first question mark will be replaced with the value in params orders and the second will be replaced with true and this will find the first record in the table that has <em>2</em> as its value for the orders_count field and <em>false</em> for its locked field.</p></div>
+<h3 id="_pure_string_conditions">5.1. Pure String Conditions</h3>
+<div class="para"><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>
+<h3 id="_array_conditions">5.2. Array Conditions</h3>
+<div class="literalblock">
+<div class="content">
+<pre><tt>Now what if that number could vary, say as a parameter from somewhere, or perhaps from the user's level status somewhere? The find then becomes something like +Client.first(:conditions => ["orders_count = ?", params[:orders]])+. Active Record will go through the first element in the conditions value and any additional elements will replace the question marks (?) in the first element. If you want to specify two conditions, you can do it like +Client.first(:conditions => ["orders_count = ? AND locked = ?", params[:orders], false])+. In this example, the first question mark will be replaced with the value in params orders and the second will be replaced with true and this will find the first record in the table that has '2' as its value for the orders_count field and 'false' for its locked field.</tt></pre>
+</div></div>
<div class="para"><p>The reason for doing code like:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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>`Client<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>first<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_count = ?"</span><span style="color: #990000">,</span> params<span style="color: #990000">[:</span>orders<span style="color: #990000">]])</span>`
+<pre><tt><span style="color: #990000">+</span>Client<span style="color: #990000">.</span>first<span style="color: #990000">(:</span>conditions <span style="color: #990000">=></span> <span style="color: #990000">[</span><span style="color: #FF0000">"orders_count = ?"</span><span style="color: #990000">,</span> params<span style="color: #990000">[:</span>orders<span style="color: #990000">]])+</span>
</tt></pre></div></div>
<div class="para"><p>instead of:</p></div>
-<div class="para"><p><tt>Client.find(:first, :conditions ⇒ "orders_count = #{params[:orders]}")</tt></p></div>
-<div class="para"><p>is because of parameter safety. Putting the variable directly into the conditions string will parse the variable <strong>as-is</strong>. This means that it will be an unescaped variable directly from a user who may have malicious intent. If you do this, you put your entire database at risk because once a user finds out he or she can exploit your database they can do just about anything to it. Never ever put your parameters directly inside the conditions string.</p></div>
-<div class="para"><p>If you're looking for a range inside of a table for example users created in a certain timeframe you can use the conditions option coupled with the IN sql statement for this. If we had two dates coming in from a controller we could do something like this to look for a range:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<div class="listingblock">
+<div class="content">
+<pre><tt>+Client.first(:conditions => "orders_count = #{params[:orders]}")+</tt></pre>
+</div></div>
+<div class="para"><p>is because of parameter safety. Putting the variable directly into the conditions string will pass the variable to the database <strong>as-is</strong>. This means that it will be an unescaped variable directly from a user who may have malicious intent. If you do this, you put your entire database at risk because once a user finds out he or she can exploit your database they can do just about anything to it. Never ever put your parameters directly inside the conditions string.</p></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/tip.png" alt="Tip" />
+</td>
+<td class="content">For more information on the dangers of SQL injection, see the <a href="../security.html#_sql_injection">Ruby on Rails Security Guide</a>.</td>
+</tr></table>
+</div>
+<div class="para"><p>If you're looking for a range inside of a table (for example, users created in a certain timeframe) you can use the conditions option coupled with the IN sql statement for this. If you had two dates coming in from a controller you could do something like this to look for a range:</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>Client<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<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">"created_at IN (?)"</span><span style="color: #990000">,</span> <span style="color: #990000">(</span>params<span style="color: #990000">[:</span>start_date<span style="color: #990000">].</span>to_date<span style="color: #990000">)..(</span>params<span style="color: #990000">[:</span>end_date<span style="color: #990000">].</span>to_date<span style="color: #990000">)])</span>
+<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 IN (?)"</span><span style="color: #990000">,</span>
+ <span style="color: #990000">(</span>params<span style="color: #990000">[:</span>start_date<span style="color: #990000">].</span>to_date<span style="color: #990000">)..(</span>params<span style="color: #990000">[:</span>end_date<span style="color: #990000">].</span>to_date<span style="color: #990000">)])</span>
</tt></pre></div></div>
<div class="para"><p>This would generate the proper query which is great for small ranges but not so good for larger ranges. For example if you pass in a range of date objects spanning a year that's 365 (or possibly 366, depending on the year) strings it will attempt to match your field against.</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> <span style="color: #FF0000">`users`</span> <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>created_at <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span> <span style="color: #990000">(</span><span style="color: #FF0000">'2007-12-31'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-01'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-02'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-03'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-04'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-05'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-06'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-07'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-08'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-09'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-10'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-11'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-12'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-13'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-14'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-15'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-16'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-17'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-18'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-19'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-20'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-21'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-22'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-23'</span><span style="color: #990000">,...</span>
-<span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">15</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">16</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">17</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">18</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">19</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">20</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">21</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">22</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">23</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">24</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">25</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">26</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">27</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">28</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">29</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">30</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">31</span><span style="color: #FF0000">'))</span>
+<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: #990000">+</span>users<span style="color: #990000">+</span> <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>created_at <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span>
+ <span style="color: #990000">(</span><span style="color: #FF0000">'2007-12-31'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-01'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-02'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-03'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-04'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-05'</span><span style="color: #990000">,</span>
+ <span style="color: #FF0000">'2008-01-06'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-07'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-08'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-09'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-10'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-11'</span><span style="color: #990000">,</span>
+ <span style="color: #FF0000">'2008-01-12'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-13'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-14'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-15'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-16'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-17'</span><span style="color: #990000">,</span>
+ <span style="color: #FF0000">'2008-01-18'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-19'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-20'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-21'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-22'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-23'</span><span style="color: #990000">,...</span>
+ ‘<span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">15</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">16</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">17</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">18</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">19</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">20</span><span style="color: #FF0000">',</span>
+<span style="color: #FF0000"> '</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">21</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">22</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">23</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">24</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">25</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">26</span><span style="color: #FF0000">',</span>
+<span style="color: #FF0000"> '</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">27</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">28</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">29</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">30</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">31</span><span style="color: #FF0000">'))</span>
</tt></pre></div></div>
<div class="para"><p>Things can get <strong>really</strong> messy if you pass in time objects as it will attempt to compare your field to <strong>every second</strong> in that range:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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>Client<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<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">"created_at IN (?)"</span><span style="color: #990000">,</span> <span style="color: #990000">(</span>params<span style="color: #990000">[:</span>start_date<span style="color: #990000">].</span>to_date<span style="color: #990000">.</span>to_time<span style="color: #990000">)..(</span>params<span style="color: #990000">[:</span>end_date<span style="color: #990000">].</span>to_date<span style="color: #990000">.</span>to_time<span style="color: #990000">)])</span>
+<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 IN (?)"</span><span style="color: #990000">,</span>
+ <span style="color: #990000">(</span>params<span style="color: #990000">[:</span>start_date<span style="color: #990000">].</span>to_date<span style="color: #990000">.</span>to_time<span style="color: #990000">)..(</span>params<span style="color: #990000">[:</span>end_date<span style="color: #990000">].</span>to_date<span style="color: #990000">.</span>to_time<span style="color: #990000">)])</span>
</tt></pre></div></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> <span style="color: #FF0000">`users`</span> <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>created_at <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span> <span style="color: #990000">(</span><span style="color: #FF0000">'2007-12-01 00:00:00'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'2007-12-01 00:00:01'</span> <span style="color: #990000">...</span> <span style="color: #FF0000">'2007-12-01 23:59:59'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'2007-12-02 00:00:00'</span><span style="color: #990000">))</span>
+<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: #990000">+</span>users<span style="color: #990000">+</span> <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>created_at <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span>
+ <span style="color: #990000">(</span><span style="color: #FF0000">'2007-12-01 00:00:00'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'2007-12-01 00:00:01'</span> <span style="color: #990000">...</span>
+ <span style="color: #FF0000">'2007-12-01 23:59:59'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'2007-12-02 00:00:00'</span><span style="color: #990000">))</span>
</tt></pre></div></div>
<div class="para"><p>This could possibly cause your database server to raise an unexpected error, for example MySQL will throw back this error:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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>Got a packet bigger than 'max_allowed_packet' bytes: <query>
+<pre><tt>Got a packet bigger than 'max_allowed_packet' bytes: _query_
</tt></pre></div></div>
-<div class="para"><p>Where <query> is the actual query used to get that error.</p></div>
+<div class="para"><p>Where <em>query</em> is the actual query used to get that error.</p></div>
<div class="para"><p>In this example it would be better to use greater-than and less-than operators in SQL, like so:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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>Client<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">,</span> <span style="color: #990000">:</span>condtions <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>
+<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="para"><p>You can also use the greater-than-or-equal-to and less-than-or-equal-to like this:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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>Client<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">,</span> <span style="color: #990000">:</span>condtions <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>
+<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="para"><p>Just like in Ruby.</p></div>
+<h3 id="_hash_conditions">5.3. Hash Conditions</h3>
+<div class="para"><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
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+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="para"><p>This makes for clearer readability if you have a large number of variable conditions.</p></div>
</div>
<h2 id="_ordering">6. Ordering</h2>
<div class="sectionbody">
-<div class="para"><p>If you're getting a set of records and want to force an order, you can use <tt>Client.find(:all, :order ⇒ "created_at")</tt> which by default will sort the records by ascending order. If you'd like to order it in descending order, just tell it to do that using <tt>Client.find(:all, :order ⇒ "created_at desc")</tt></p></div>
+<div class="para"><p>If you're getting a set of records and want to force an order, you can use <tt>Client.all(:order ⇒ "created_at")</tt> which by default will sort the records by ascending order. If you'd like to order it in descending order, just tell it to do that using <tt>Client.all(:order ⇒ "created_at desc")</tt></p></div>
</div>
<h2 id="_selecting_certain_fields">7. Selecting Certain Fields</h2>
<div class="sectionbody">
-<div class="para"><p>To select certain fields, you can use the select option like this: <tt>Client.find(: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 0,1</tt> on your database.</p></div>
+<div class="para"><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 0,1</tt> on your database.</p></div>
</div>
<h2 id="_limit_amp_offset">8. Limit & Offset</h2>
<div class="sectionbody">
<div class="para"><p>If you want to limit the amount of records to a certain subset of all the records retreived you usually use limit for this, sometimes coupled with offset. Limit is the maximum number of records that will be retreived 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="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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>Client<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">,</span> <span style="color: #990000">:</span>limit <span style="color: #990000">=></span> <span style="color: #993399">5</span><span style="color: #990000">)</span>
+<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>limit <span style="color: #990000">=></span> <span style="color: #993399">5</span><span style="color: #990000">)</span>
</tt></pre></div></div>
-<div class="para"><p>This code will return a maximum of 5 clients and because we've specified no offset it will return the first 5 clients in the table. The SQL it executes will look like this:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<div class="para"><p>This code will return a maximum of 5 clients and because it specifies no offset it will return the first 5 clients in the table. The SQL it executes will look like this:</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-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>
</tt></pre></div></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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>Client<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">,</span> <span style="color: #990000">:</span>limit <span style="color: #990000">=></span> <span style="color: #993399">5</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>offset <span style="color: #990000">=></span> <span style="color: #993399">5</span><span style="color: #990000">)</span>
+<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>limit <span style="color: #990000">=></span> <span style="color: #993399">5</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>offset <span style="color: #990000">=></span> <span style="color: #993399">5</span><span style="color: #990000">)</span>
</tt></pre></div></div>
-<div class="para"><p>This code will return a maximum of 5 clients and because we have specified an offset this time, it will return these records starting from the 5th client in the clients table. The SQL looks like:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<div class="para"><p>This code will return a maximum of 5 clients and because it specifies an offset this time, it will return these records starting from the 5th client in the clients table. The SQL looks like:</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 -->
@@ -557,50 +592,50 @@ http://www.gnu.org/software/src-highlite --> </div>
<h2 id="_group">9. Group</h2>
<div class="sectionbody">
-<div class="para"><p>The group option for find is useful, for example, if you want to find a collection of the dates orders were created on. We could use the option in this context:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<div class="para"><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">
+<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>Order<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">,</span> <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>order <span style="color: #990000">=></span> <span style="color: #FF0000">"created_at"</span><span style="color: #990000">)</span>
+<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>order <span style="color: #990000">=></span> <span style="color: #FF0000">"created_at"</span><span style="color: #990000">)</span>
</tt></pre></div></div>
-<div class="para"><p>And this will give us a single Order object for each date that we have orders in our database.</p></div>
+<div class="para"><p>And this will give you a single <tt>Order</tt> object for each date where there are orders in the database.</p></div>
<div class="para"><p>The SQL that would be executed would be something like this:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> <span style="color: #FF0000">`orders`</span> <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>
+<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: #990000">+</span>orders<span style="color: #990000">+</span> <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="_read_only">10. Read Only</h2>
<div class="sectionbody">
-<div class="para"><p>Readonly is a find 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 <tt>ActiveRecord::ReadOnlyRecord</tt> error. To set this option, specify it like this:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<div class="para"><p>Readonly is a find 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 <tt>Active Record::ReadOnlyRecord</tt> error. To set this option, specify it like this:</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>Client<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>first<span style="color: #990000">,</span> <span style="color: #990000">:</span>readonly <span style="color: #990000">=></span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">)</span>
+<pre><tt>Client<span style="color: #990000">.</span>first<span style="color: #990000">(:</span>readonly <span style="color: #990000">=></span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">)</span>
</tt></pre></div></div>
-<div class="para"><p>If you assign this record to a variable <tt>client</tt> calling the following code will raise an ActiveRecord::ReadOnlyRecord:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<div class="para"><p>If you assign this record to a variable <tt>client</tt>, calling the following code will raise an ActiveRecord::ReadOnlyRecord:</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>client <span style="color: #990000">=</span> Client<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>first<span style="color: #990000">,</span> <span style="color: #990000">:</span>readonly <span style="color: #990000">=></span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">)</span>
+<pre><tt>client <span style="color: #990000">=</span> Client<span style="color: #990000">.</span>first<span style="color: #990000">(:</span>readonly <span style="color: #990000">=></span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">)</span>
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>
<div class="sectionbody">
-<div class="para"><p>If you're wanting to stop race conditions for a specific record, say for example you're incrementing a single field for a record you can use the lock option to ensure that the record is updated correctly. It's recommended this be used inside a transaction.</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<div class="para"><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">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
@@ -612,56 +647,71 @@ http://www.gnu.org/software/src-highlite --> </div>
<h2 id="_making_it_all_work_together">12. Making It All Work Together</h2>
<div class="sectionbody">
-<div class="para"><p>You can chain these options together in no particular order as ActiveRecord will write the correct SQL for you. If you specify two instances of the same options inside the find statement ActiveRecord will use the latter.</p></div>
+<div class="para"><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 find statement ActiveRecord will use the latter.</p></div>
</div>
<h2 id="_eager_loading">13. Eager Loading</h2>
<div class="sectionbody">
-<div class="para"><p>Eager loading is loading associated records along with any number of records in as few queries as possible. Lets say for example if we wanted to load all the addresses associated with all the clients all in the same query we would use <tt>Client.find(:all, :include ⇒ :address)</tt>. If we wanted to include both the address and mailing address for the client we would use `Client.find(:all), :include ⇒ [:address, :mailing_address]). Inclue 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="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<div class="para"><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 +Client.find(:all), :include ⇒ [:address, :mailing_address]). 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">
+<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>Client <span style="font-weight: bold"><span style="color: #0000FF">Load</span></span> <span style="color: #990000">(</span><span style="color: #993399">0.000383</span><span style="color: #990000">)</span> <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
-Address <span style="font-weight: bold"><span style="color: #0000FF">Load</span></span> <span style="color: #990000">(</span><span style="color: #993399">0.119770</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> addresses<span style="color: #990000">.*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> addresses <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>addresses<span style="color: #990000">.</span>client_id <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span> <span style="color: #990000">(</span><span style="color: #993399">13</span><span style="color: #990000">,</span><span style="color: #993399">14</span><span style="color: #990000">))</span>
-MailingAddress <span style="font-weight: bold"><span style="color: #0000FF">Load</span></span> <span style="color: #990000">(</span><span style="color: #993399">0.001985</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> mailing_addresses<span style="color: #990000">.*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> mailing_addresses <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>mailing_addresses<span style="color: #990000">.</span>client_id <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span> <span style="color: #990000">(</span><span style="color: #993399">13</span><span style="color: #990000">,</span><span style="color: #993399">14</span><span style="color: #990000">))</span>
+Address <span style="font-weight: bold"><span style="color: #0000FF">Load</span></span> <span style="color: #990000">(</span><span style="color: #993399">0.119770</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> addresses<span style="color: #990000">.*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> addresses
+ <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>addresses<span style="color: #990000">.</span>client_id <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span> <span style="color: #990000">(</span><span style="color: #993399">13</span><span style="color: #990000">,</span><span style="color: #993399">14</span><span style="color: #990000">))</span>
+MailingAddress <span style="font-weight: bold"><span style="color: #0000FF">Load</span></span> <span style="color: #990000">(</span><span style="color: #993399">0.001985</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> mailing_addresses<span style="color: #990000">.*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span>
+ mailing_addresses <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>mailing_addresses<span style="color: #990000">.</span>client_id <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span> <span style="color: #990000">(</span><span style="color: #993399">13</span><span style="color: #990000">,</span><span style="color: #993399">14</span><span style="color: #990000">))</span>
+</tt></pre></div></div>
+<div class="para"><p>The numbers <tt>13</tt> and <tt>14</tt> in the above SQL are the ids of the clients gathered from the <tt>Client.all</tt> query. Rails will then run a query to gather all the addresses and mailing addresses that have a client_id of 13 or 14. Although this is done in 3 queries, this is more efficient than not eager loading because without eager loading it would run a query for every time you called <tt>address</tt> or <tt>mailing_address</tt> on one of the objects in the clients array, which may lead to performance issues if you're loading a large number of records at once.</p></div>
+<div class="para"><p>If you wanted to get all the addresses for a client in the same query you would do <tt>Client.all(:joins ⇒ :address)</tt> and you wanted to find the address and mailing address for that client you would do <tt>Client.all(:joins ⇒ [:address, :mailing_address])</tt>. This is more efficient because it does all the SQL in one query, as shown by this example:</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>Client <span style="font-weight: bold"><span style="color: #0000FF">Load</span></span> <span style="color: #990000">(</span><span style="color: #993399">0.000455</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> clients<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">INNER</span></span> <span style="font-weight: bold"><span style="color: #0000FF">JOIN</span></span> addresses
+ <span style="font-weight: bold"><span style="color: #0000FF">ON</span></span> addresses<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">INNER</span></span> <span style="font-weight: bold"><span style="color: #0000FF">JOIN</span></span> mailing_addresses <span style="font-weight: bold"><span style="color: #0000FF">ON</span></span>
+ mailing_addresses<span style="color: #990000">.</span>client_id <span style="color: #990000">=</span> client<span style="color: #990000">.</span>id
</tt></pre></div></div>
-<div class="para"><p>The numbers <tt>13</tt> and <tt>14</tt> in the above SQL are the ids of the clients gathered from the <tt>Client.find(:all)</tt> query. Rails will then run a query to gather all the addresses and mailing addresses that have a client_id of 13 or 14. Although this is done in 3 queries, this is more efficient than not eager loading because without eager loading it would run a query for every time you called <tt>address</tt> or <tt>mailing_address</tt> on one of the objects in the clients array, which may lead to performance issues if you're loading a large number of records at once.</p></div>
-<div class="para"><p>An alternative (and more efficient) way to do eager loading is to use the joins option. For example if we wanted to get all the addresses for a client we would do <tt>Client.find(:all, :joins ⇒ :address)</tt> and if we wanted to find the address and mailing address for that client we would do <tt>Client.find(:all, :joins ⇒ [:address, :mailing_address])</tt>. This is more efficient because it does all the SQL in one query, as shown by this example:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<div class="para"><p>This query is more efficent, but there's a gotcha: if you have a client who does not have an address or a mailing address they will not be returned in this query at all. If you have any association as an optional association, you may want to use include rather than joins. Alternatively, you can use a SQL join clause to specify exactly the join you need (Rails always assumes an inner join):</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: #FF0000">`Client Load (0.000455) SELECT clients.* FROM clients INNER JOIN addresses ON addresses.client_id = client.id INNER JOIN mailing_addresses ON mailing_addresses.client_id = client.id</span>
+<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>joins <span style="color: #990000">=></span> “LEFT OUTER JOIN addresses ON
+ client<span style="color: #990000">.</span>id <span style="color: #990000">=</span> addresses<span style="color: #990000">.</span>client_id LEFT OUTER JOIN mailing_addresses ON
+ client<span style="color: #990000">.</span>id <span style="color: #990000">=</span> mailing_addresses<span style="color: #990000">.</span>client_id”<span style="color: #990000">)</span>
</tt></pre></div></div>
-<div class="para"><p>This query is more efficent, but there's a gotcha. If you have a client who does not have an address or a mailing address they will not be returned in this query at all. If you have any association as an optional association, you may want to use include rather than joins.</p></div>
<div class="para"><p>When using eager loading you can specify conditions for the columns of the tables inside the eager loading to get back a smaller subset. If, for example, you want to find a client and all their orders within the last two weeks you could use eager loading with conditions for this:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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>Client<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>first<span style="color: #990000">,</span> <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> Time<span style="color: #990000">.</span>now <span style="color: #990000">-</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>weeks<span style="color: #990000">,</span> Time<span style="color: #990000">.</span>now<span style="color: #990000">])</span>
+<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> Time<span style="color: #990000">.</span>now <span style="color: #990000">-</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>weeks<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>
<div class="sectionbody">
-<div class="para"><p>With every field (also known as an attribute) you define in your table, ActiveRecord provides finder methods for these. 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 ActiveRecord. 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>. If you want to find both by name and locked, you can chain these finders together by simply typing and between the fields for example <tt>Client.find_by_name_and_locked(<em>Ryan</em>, true)</tt>. These finders are an excellent alternative to using the conditions option, mainly because it's shorter to type <tt>find_by_name(params[:name])</tt> than it is to type <tt>find(:first, :conditions ⇒ ["name = ?", params[:name]])</tt>.</p></div>
-<div class="para"><p>There's another set of dynamic finders that let you find or create/initialize objects if they aren't find. These work in a similar fashion to the other finders and can be used like <tt>find_or_create_by_name(params[:name])</tt>. Using this will firstly perform a find and then create if the find returns nil, the SQL looks like this for <tt>Client.find_or_create_by_name(<em>Ryan</em>)</tt>:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<div class="para"><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>. If you want to find both by name and locked, you can chain these finders together by simply typing <tt>and</tt> between the fields for example <tt>Client.find_by_name_and_locked(<em>Ryan</em>, true)</tt>. These finders are an excellent alternative to using the conditions option, mainly because it's shorter to type <tt>find_by_name(params[:name])</tt> than it is to type <tt>first(:conditions ⇒ ["name = ?", params[:name]])</tt>.</p></div>
+<div class="para"><p>There's another set of dynamic finders that let you find or create/initialize objects if they aren't find. These work in a similar fashion to the other finders and can be used like <tt>find_or_create_by_name(params[:name])</tt>. Using this will firstly perform a find and then create if the find returns nil. The SQL looks like this for <tt>Client.find_or_create_by_name(<em>Ryan</em>)</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-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">`name`</span> <span style="color: #990000">=</span> <span style="color: #FF0000">'Ryan'</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">1</span>
+<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: #990000">+</span>clients<span style="color: #990000">+</span> <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(+</span>clients<span style="color: #990000">+.+</span>name<span style="color: #990000">+</span> <span style="color: #990000">=</span> <span style="color: #FF0000">'Ryan'</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">1</span>
BEGIN
-<span style="font-weight: bold"><span style="color: #0000FF">INSERT</span></span> <span style="font-weight: bold"><span style="color: #0000FF">INTO</span></span> <span style="color: #FF0000">`clients`</span> <span style="color: #990000">(</span><span style="color: #FF0000">`name`</span><span style="color: #990000">,</span> <span style="color: #FF0000">`updated_at`</span><span style="color: #990000">,</span> <span style="color: #FF0000">`created_at`</span><span style="color: #990000">,</span> <span style="color: #FF0000">`orders_count`</span><span style="color: #990000">,</span> <span style="color: #FF0000">`locked`</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">VALUES</span></span><span style="color: #990000">(</span><span style="color: #FF0000">'Ryan'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'2008-09-28 15:39:12'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'2008-09-28 15:39:12'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'0'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'0'</span><span style="color: #990000">)</span>
+<span style="font-weight: bold"><span style="color: #0000FF">INSERT</span></span> <span style="font-weight: bold"><span style="color: #0000FF">INTO</span></span> <span style="color: #990000">+</span>clients<span style="color: #990000">+</span> <span style="color: #990000">(+</span>name<span style="color: #990000">+,</span> <span style="color: #990000">+</span>updated_at<span style="color: #990000">+,</span> <span style="color: #990000">+</span>created_at<span style="color: #990000">+,</span> <span style="color: #990000">+</span>orders_count<span style="color: #990000">+,</span> <span style="color: #990000">+</span>locked<span style="color: #990000">+)</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">VALUES</span></span><span style="color: #990000">(</span><span style="color: #FF0000">'Ryan'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'2008-09-28 15:39:12'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'2008-09-28 15:39:12'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'0'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'0'</span><span style="color: #990000">)</span>
COMMIT
</tt></pre></div></div>
-<div class="para"><p><tt>find_or_create</tt>'s sibling, find_or_initialize, will find an object and if it does not exist will call <tt>new</tt> with the parameters you passed in. For example:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<div class="para"><p><tt>find_or_create</tt>'s sibling, <tt>find_or_initialize</tt>, will find an object and if it does not exist will call <tt>new</tt> with the parameters you passed in. For example:</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 -->
@@ -671,25 +721,36 @@ http://www.gnu.org/software/src-highlite --> </div>
<h2 id="_finding_by_sql">15. Finding By SQL</h2>
<div class="sectionbody">
-<div class="para"><p>If you'd like to use your own SQL to find records a table you can use <tt>find_by_sql</tt>. <tt>find_by_sql</tt> will return an array of objects even if it only returns a single record in it's call to the database. For example you could run this query:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<div class="para"><p>If you'd like to use your own SQL to find records a table you can use <tt>find_by_sql</tt>. The <tt>find_by_sql</tt> method will return an array of objects even if it only returns a single record in it's call to the database. For example you could run this query:</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>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="para"><p><tt>find_by_sql</tt> provides you with a simple way of making custom calls to the database and converting those to objects.</p></div>
+<div class="para"><p><tt>find_by_sql</tt> provides you with a simple way of making custom calls to the database and retreiving instantiated objects.</p></div>
+</div>
+<h2 id="_tt_select_all_tt">16. <tt>select_all</tt></h2>
+<div class="sectionbody">
+<div class="para"><p><tt>find_by_sql</tt> has a close relative called <tt>select_all</tt>. <tt>select_all</tt> will retreive 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">
+<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>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">16. Working with Associations</h2>
+<h2 id="_working_with_associations">17. Working with Associations</h2>
<div class="sectionbody">
<div class="para"><p>When you define a has_many association on a model you get the find method and dynamic finders also on that association. This is helpful for finding associated records within the scope of an exisiting 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 controllers.</p></div>
</div>
-<h2 id="_named_scopes">17. Named Scopes</h2>
+<h2 id="_named_scopes">18. Named Scopes</h2>
<div class="sectionbody">
-<div class="para"><p>In this section we'll cover adding named scopes to the models in the application. Let's say we want to find all clients who are male we would use this code:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<div class="para"><p>Named scopes are another way to add custom finding behavior to the models in the application. Suppose want to find all clients who are male. Yould use this code:</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 -->
@@ -697,10 +758,10 @@ http://www.gnu.org/software/src-highlite --> named_scope <span style="color: #990000">:</span>males<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>gender <span style="color: #990000">=></span> <span style="color: #FF0000">"male"</span> <span style="color: #FF0000">}</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
</tt></pre></div></div>
-<div class="para"><p>And we could call it like <tt>Client.males</tt> to get all the clients who are male.</p></div>
-<div class="para"><p>If we wanted to find all the clients who are active, we could use this:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<div class="para"><p>And you could call it like <tt>Client.males.all</tt> to get all the clients who are male. Please note that if you do not specify the <tt>all</tt> on the end you will get a <tt>Scope</tt> object back, not a set of records which you do get back if you put the <tt>all</tt> on the end.</p></div>
+<div class="para"><p>If you wanted to find all the clients who are active, you could use this:</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 -->
@@ -708,26 +769,26 @@ 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="para"><p>We would call this new named_scope by doing <tt>Client.active</tt> and this will do the same query as if we just used <tt>Client.find(:all, :conditions ⇒ ["active = ?", true])</tt>. Please be aware that the conditions syntax in named_scope and find is different and the two are not interchangeable. If you want to find the first client within this named scope you could do <tt>Client.active.first</tt>.</p></div>
-<div class="para"><p>and then if we wanted to find all the clients who are active and male we could stack the named scopes like this:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<div class="para"><p>You can call this new named_scope by doing <tt>Client.active.all</tt> and this will do the same query as if we just used <tt>Client.all(:conditions ⇒ ["active = ?", true])</tt>. Please be aware that the conditions syntax in named_scope and find is different and the two are not interchangeable. If you want to find the first client within this named scope you could do <tt>Client.active.first</tt>.</p></div>
+<div class="para"><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
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
+<pre><tt>Client<span style="color: #990000">.</span>males<span style="color: #990000">.</span>active<span style="color: #990000">.</span>all
</tt></pre></div></div>
-<div class="para"><p>If you would then like to do a <tt>find</tt> on that subset of clients, you can. Just like an association, named scopes allow you to call <tt>find</tt> on a set of records:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<div class="para"><p>If you would then like to do a <tt>all</tt> on that scope, you can. Just like an association, named scopes allow you to call <tt>all</tt> on them:</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>Client<span style="color: #990000">.</span>males<span style="color: #990000">.</span>active<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<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">"age > ?"</span><span style="color: #990000">,</span> params<span style="color: #990000">[:</span>age<span style="color: #990000">]])</span>
+<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>
-<div class="para"><p>Now observe the following code:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<div class="para"><p>Consider the following code:</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 -->
@@ -735,9 +796,9 @@ http://www.gnu.org/software/src-highlite --> named_scope <span style="color: #990000">:</span>recent<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>created_at <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: #FF0000">}</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
</tt></pre></div></div>
-<div class="para"><p>What we see here is what looks to be a standard named scope that defines a method called recent which gathers all records created any time between now and 2 weeks ago. That's correct for the first time the model is loaded but for any time after that, <tt>2.weeks.ago</tt> is set to that same value, so you will consistently get records from a certain date until your model is reloaded by something like your application restarting. The way to fix this is to put the code in a lambda block:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<div class="para"><p>This looks like a standard named scope that defines a method called recent which gathers all records created any time between now and 2 weeks ago. That's correct for the first time the model is loaded but for any time after that, <tt>2.weeks.ago</tt> is set to that same value, so you will consistently get records from a certain date until your model is reloaded by something like your application restarting. The way to fix this is to put the code in a lambda block:</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 -->
@@ -745,54 +806,67 @@ 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="para"><p>And now every time the recent named scope is called, because it's wrapped in a lambda block this code will be parsed every time 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>
+<div class="para"><p>And now every time the recent named scope is called, the code in the lambda block will be parsed, 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>
<div class="para"><p>In a named scope you can use <tt>:include</tt> and <tt>:joins</tt> options just like in find.</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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> Client <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
- named_scope <span style="color: #990000">:</span>active_within_2_weeks<span style="color: #990000">,</span> <span style="color: #990000">:</span>joins <span style="color: #990000">=></span> <span style="color: #990000">:</span>order<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">"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>
+ named_scope <span style="color: #990000">:</span>active_within_2_weeks<span style="color: #990000">,</span> <span style="color: #990000">:</span>joins <span style="color: #990000">=></span> <span style="color: #990000">:</span>order<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">"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="para"><p>This method called as <tt>Client.active_within_2_weeks</tt> will return all clients who have placed orders in the past 2 weeks.</p></div>
+<div class="para"><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>
<div class="para"><p>If you want to pass a named scope a compulsory argument, just specify it as a block parameter like this:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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> Client <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
+ named_scope <span style="color: #990000">:</span>recent<span style="color: #990000">,</span> lambda <span style="color: #FF0000">{</span> <span style="color: #990000">|</span>time<span style="color: #990000">|</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> time<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="para"><p>This will work if you call <tt>Client.recent(2.weeks.ago).all</tt> but not if you call <tt>Client.recent</tt>. If you want to add an optional argument for this, you have to use the splat operator as the block's parameter.</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-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
- named_scope <span style="color: #990000">:</span>recent<span style="color: #990000">,</span> lambda <span style="color: #FF0000">{</span> <span style="color: #990000">|</span>time<span style="color: #990000">|</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> time<span style="color: #990000">]</span> <span style="color: #FF0000">}</span> <span style="color: #FF0000">}</span> <span style="color: #FF0000">}</span>
+ named_scope <span style="color: #990000">:</span>recent<span style="color: #990000">,</span> lambda <span style="color: #FF0000">{</span> <span style="color: #990000">|*</span>args<span style="color: #990000">|</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> args<span style="color: #990000">.</span>first <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="para"><p>This will work if we call <tt>Client.recent(2.weeks.ago)</tt> but not if we call <tt>Client.recent</tt>. If we want to add an optional argument for this, we have to use the splat operator as the block's parameter.</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<div class="para"><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="para"><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>
+<div class="para"><p>Finally, if you wish to define named scopes on the fly you can use the scoped method:</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-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
- named_scope <span style="color: #990000">:</span>recent<span style="color: #990000">,</span> lambda <span style="color: #FF0000">{</span> <span style="color: #990000">|*</span>args<span style="color: #990000">|</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> args<span style="color: #990000">.</span>first <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="color: #FF0000">}</span>
+ <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>recent
+ scoped <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="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="para"><p>This will work with <tt>Client.recent(2.weeks.ago)</tt> and <tt>Client.recent</tt> with the latter always returning records with a created_at date between right now and 2 weeks ago.</p></div>
-<div class="para"><p>Remember that named scopes are stackable, so you will be able to do <tt>Client.recent(2.weeks.ago).unlocked</tt> to find all clients created between right now and 2 weeks ago and have their locked field set to false.</p></div>
</div>
-<h2 id="_existance_of_objects">18. Existance of Objects</h2>
+<h2 id="_existence_of_objects">19. Existence of Objects</h2>
<div class="sectionbody">
-<div class="para"><p>If you simply want to check for the existance of the object there's a method called <tt>exists?</tt>. This method will query the database using the same query as find, but instead of returning an object or collection of objects it will return either true or false.</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<div class="para"><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 find, but instead of returning an object or collection of objects it will return either true or false.</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>Client<span style="color: #990000">.</span>exists?<span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">)</span>
</tt></pre></div></div>
<div class="para"><p>The above code will check for the existance of a clients table record with the id of 1 and return true if it exists.</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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 -->
@@ -800,90 +874,92 @@ http://www.gnu.org/software/src-highlite --> <span style="font-style: italic"><span style="color: #9A1900"># or</span></span>
Client<span style="color: #990000">.</span>exists?<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 class="para"><p><tt>exists?</tt> also takes multiple ids, as shown by the above code, but the catch is that it will return true if any one of those records exists.</p></div>
+<div class="para"><p>The <tt>exists?</tt> method also takes multiple ids, as shown by the above code, but the catch is that it will return true if any one of those records exists.</p></div>
<div class="para"><p>Further more, <tt>exists</tt> takes a <tt>conditions</tt> option much like find:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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>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">19. Calculations</h2>
+<h2 id="_calculations">20. Calculations</h2>
<div class="sectionbody">
<div class="para"><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="para"><p><tt>count</tt> takes conditions much in the same way <tt>exists?</tt> does:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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>Client<span style="color: #990000">.</span>count<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 class="para"><p>Which will execute:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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">SELECT</span></span> count<span style="color: #990000">(*)</span> <span style="font-weight: bold"><span style="color: #0000FF">AS</span></span> count_all <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>first_name <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">)</span>
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> count<span style="color: #990000">(*)</span> <span style="font-weight: bold"><span style="color: #0000FF">AS</span></span> count_all <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> <span style="color: #990000">+</span>clients<span style="color: #990000">+</span> <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>first_name <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">)</span>
</tt></pre></div></div>
<div class="para"><p>You can also use <tt>include</tt> or <tt>joins</tt> for this to do something a little more complex:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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>Client<span style="color: #990000">.</span>count<span style="color: #990000">(:</span>conditions <span style="color: #990000">=></span> <span style="color: #FF0000">"clients.first_name = 'Ryan' AND orders.status = 'received'"</span><span style="color: #990000">,</span> <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>
</tt></pre></div></div>
<div class="para"><p>Which will execute:</p></div>
-<div class="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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">SELECT</span></span> count<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">DISTINCT</span></span> <span style="color: #FF0000">`clients`</span><span style="color: #990000">.</span>id<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">AS</span></span> count_all <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">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> <span style="color: #FF0000">`orders`</span> <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">'name'</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>
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> count<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">DISTINCT</span></span> <span style="color: #990000">+</span>clients<span style="color: #990000">+.</span>id<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">AS</span></span> count_all <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> <span style="color: #990000">+</span>clients<span style="color: #990000">+</span>
+ <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> <span style="color: #990000">+</span>orders<span style="color: #990000">+</span> <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">'name'</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="para"><p>We specify <tt>clients.first_name</tt> just in case one of our join tables has a field also called <tt>first_name</tt> and we do <tt>orders.status</tt> because that's the name of our join table.</p></div>
-<h3 id="_count">19.1. Count</h3>
+<div class="para"><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>
<div class="para"><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="para"><p>For options, please see the parent section, Calculations.</p></div>
-<h3 id="_average">19.2. Average</h3>
+<h3 id="_average">20.2. Average</h3>
<div class="para"><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="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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>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="para"><p>This will return a number (possibly a floating point number such as 3.14159265) representing the average of the fields.</p></div>
+<div class="para"><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="para"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a></p></div>
-<h3 id="_minimum">19.3. Minimum</h3>
+<h3 id="_minimum">20.3. Minimum</h3>
<div class="para"><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="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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>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="para"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a></p></div>
-<h3 id="_maximum">19.4. Maximum</h3>
+<h3 id="_maximum">20.4. Maximum</h3>
<div class="para"><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="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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>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="para"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a></p></div>
-<h3 id="_sum">19.5. Sum</h3>
+<h3 id="_sum">20.5. Sum</h3>
<div class="para"><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="exampleblock">
-<div class="exampleblock-content"><!-- Generator: GNU source-highlight 2.9
+<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 -->
@@ -891,109 +967,66 @@ http://www.gnu.org/software/src-highlite --> </tt></pre></div></div>
<div class="para"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a></p></div>
</div>
-<h2 id="_credits">20. Credits</h2>
+<h2 id="_credits">21. Credits</h2>
<div class="sectionbody">
<div class="para"><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="para"><p>Thanks to Mike Gunderloy for his tips on creating this guide.</p></div>
</div>
-<h2 id="_change_log">21. Change Log</h2>
+<h2 id="_changelog">22. Changelog</h2>
<div class="sectionbody">
-<h3 id="_sunday_28_september_2008">21.1. Sunday, 28 September 2008</h3>
-<div class="olist"><ol>
-<li>
-<p>
-Changed "In Rails you don't have to type SQL" to "In Rails you don't usually have to type SQL"
-</p>
-</li>
-<li>
-<p>
-Inserted paragraph in dynamic finders about find_or_create and find_or_initialize
-</p>
-</li>
-<li>
-<p>
-Extended "First, Last, All" section.
-</p>
-</li>
-<li>
-<p>
-Renamed "First, Last & All" to "IDs, First, Last and All"
-</p>
-</li>
-<li>
-<p>
-Added finding by id and passing in ids to "IDs, First, Last and All"
-</p>
-</li>
-</ol></div>
-<h3 id="_wednesday_01_october_2008">21.2. Wednesday, 01 October 2008</h3>
-<div class="olist"><ol>
+<div class="para"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16">Lighthouse ticket</a></p></div>
+<div class="ilist"><ul>
<li>
<p>
-Did section on limit and offset, as well as section on readonly.
+October 27, 2008: Added scoped section, added named params for conditions and added sub-section headers for conditions section.
</p>
</li>
<li>
<p>
-Altered formatting so it doesn't look bad.
+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.
</p>
</li>
-</ol></div>
-<h3 id="_sunday_05_october_2008">21.3. Sunday, 05 October 2008</h3>
-<div class="olist"><ol>
<li>
<p>
-Extended conditions section to include IN and using operators inside the conditions.
+October 26, 2008: Editing pass by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> . First release version.
</p>
</li>
<li>
<p>
-Extended conditions section to include paragraph and example of parameter safety.
+October 22, 2008: Calculations complete, first complete draft by Ryan Bigg
</p>
</li>
<li>
<p>
-Added TODO sections.
+October 21, 2008: Extended named scope section by Ryan Bigg
</p>
</li>
-</ol></div>
-<h3 id="_monday_06_october_2008">21.4. Monday, 06 October 2008</h3>
-<div class="olist"><ol>
<li>
<p>
-Added section in Eager Loading about using conditions on tables that are not the model's own.
+October 9, 2008: Lock, count, cleanup by Ryan Bigg
</p>
</li>
-</ol></div>
-<h3 id="_thursday_09_october_2008">21.5. Thursday, 09 October 2008</h3>
-<div class="olist"><ol>
<li>
<p>
-Wrote section about lock option and tidied up "Making it all work together" section.
+October 6, 2008: Eager loading by Ryan Bigg
</p>
</li>
<li>
<p>
-Added section on using count.
+October 5, 2008: Covered conditions by Ryan Bigg
</p>
</li>
-</ol></div>
-<h3 id="_tuesday_21_october_2008">21.6. Tuesday, 21 October 2008</h3>
-<div class="olist"><ol>
<li>
<p>
-Extended named scope guide by adding :include and :joins and find sub-sections.
+October 1, 2008: Covered limit/offset, formatting changes by Ryan Bigg
</p>
</li>
-</ol></div>
-<h3 id="_wednesday_22_october_2008">21.7. Wednesday, 22 October 2008</h3>
-<div class="olist"><ol>
<li>
<p>
-Completed calculations section.
+September 28, 2008: Covered first/last/all by Ryan Bigg
</p>
</li>
-</ol></div>
+</ul></div>
</div>
</div> diff --git a/railties/doc/guides/html/getting_started_with_rails.html b/railties/doc/guides/html/getting_started_with_rails.html index d9a1779d56..797ae2fb3a 100644 --- a/railties/doc/guides/html/getting_started_with_rails.html +++ b/railties/doc/guides/html/getting_started_with_rails.html @@ -2010,6 +2010,11 @@ The <a href="http://wiki.rubyonrails.org/rails">Rails wiki</a> <div class="ilist"><ul>
<li>
<p>
+November 1, 2008: First approved version by <a href="../authors.html#mgunderloy">Mike Gunderloy</a>
+</p>
+</li>
+<li>
+<p>
October 16, 2008: Revised based on feedback from Pratik Naik by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> (not yet approved for publication)
</p>
</li>
diff --git a/railties/doc/guides/html/index.html b/railties/doc/guides/html/index.html index f84442d10c..306257678b 100644 --- a/railties/doc/guides/html/index.html +++ b/railties/doc/guides/html/index.html @@ -219,14 +219,6 @@ ul#navMain { <div class="sidebarblock">
<div class="sidebar-content">
<div class="sidebar-title"><a href="getting_started_with_rails.html">Getting Started with Rails</a></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/caution.png" alt="Caution" />
-</td>
-<td class="content"><a href="http://rails.lighthouseapp.com/projects/16213/tickets/2">Lighthouse Ticket</a></td>
-</tr></table>
-</div>
<div class="para"><p>Everything you need to know to install Rails and create your first application.</p></div>
</div></div>
<h2>Models</h2>
@@ -326,14 +318,6 @@ Enjoy.</p></div> <div class="sidebarblock">
<div class="sidebar-content">
<div class="sidebar-title"><a href="security.html">Securing Rails Applications</a></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/caution.png" alt="Caution" />
-</td>
-<td class="content"><a href="http://rails.lighthouseapp.com/projects/16213/tickets/7">Lighthouse Ticket</a></td>
-</tr></table>
-</div>
<div class="para"><p>This manual describes common security problems in web applications and how to
avoid them with Rails.</p></div>
</div></div>
diff --git a/railties/doc/guides/html/layouts_and_rendering.html b/railties/doc/guides/html/layouts_and_rendering.html index a394da71df..1c9ef8ce24 100644 --- a/railties/doc/guides/html/layouts_and_rendering.html +++ b/railties/doc/guides/html/layouts_and_rendering.html @@ -489,7 +489,17 @@ http://www.gnu.org/software/src-highlite --> <td class="content">You don't need to call <tt>to_xml</tt> on the object that you want to render. If you use the <tt>:xml</tt> option, <tt>render</tt> will automatically call <tt>to_xml</tt> for you.</td>
</tr></table>
</div>
-<h4 id="_options_for_tt_render_tt">2.2.10. Options for <tt>render</tt></h4>
+<h4 id="_rendering_vanilla_javascript">2.2.10. Rendering Vanilla JavaScript</h4>
+<div class="para"><p>Rails can render vanilla JavaScript (as an alternative to using <tt>update</tt> with n <tt>.rjs</tt> file):</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>render <span style="color: #990000">:</span>js <span style="color: #990000">=></span> <span style="color: #FF0000">"alert('Hello Rails');"</span>
+</tt></pre></div></div>
+<div class="para"><p>This will send the supplied string to the browser with a MIME type of <tt>text/javascript</tt>.</p></div>
+<h4 id="_options_for_tt_render_tt">2.2.11. Options for <tt>render</tt></h4>
<div class="para"><p>Calls to the <tt>render</tt> method generally accept four options:</p></div>
<div class="ilist"><ul>
<li>
@@ -560,7 +570,7 @@ http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite -->
<pre><tt>render <span style="color: #990000">:</span>xml <span style="color: #990000">=></span> photo<span style="color: #990000">,</span> <span style="color: #990000">:</span>location <span style="color: #990000">=></span> photo_url<span style="color: #990000">(</span>photo<span style="color: #990000">)</span>
</tt></pre></div></div>
-<h4 id="_finding_layouts">2.2.11. Finding Layouts</h4>
+<h4 id="_finding_layouts">2.2.12. Finding Layouts</h4>
<div class="para"><p>To find the current layout, Rails first looks for a file in <tt>app/views/layouts</tt> with the same base name as the controller. For example, rendering actions from the <tt>PhotosController</tt> class will use <tt>/app/views/layouts/photos.html.erb</tt>. If there is no such controller-specific layout, Rails will use <tt>/app/views/layouts/application.html.erb</tt>. If there is no <tt>.erb</tt> layout, Rails will use a <tt>.builder</tt> layout if one exists. Rails also provides several ways to more precisely assign specific layouts to individual controllers and actions.</p></div>
<h5 id="_specifying_layouts_on_a_per_controller_basis">Specifying Layouts on a per-Controller Basis</h5>
<div class="para"><p>You can override the automatic layout conventions in your controllers by using the <tt>layout</tt> declaration in the controller. For example:</p></div>
@@ -632,6 +642,7 @@ end</tt></pre> <div class="para"><p>With those declarations, the <tt>inventory</tt> layout would be used only for the <tt>index</tt> method, the <tt>product</tt> layout would be used for everything else except the <tt>rss</tt> method, and the <tt>rss</tt> method will have its layout determined by the automatic layout rules.</p></div>
<h5 id="_layout_inheritance">Layout Inheritance</h5>
<div class="para"><p>Layouts are shared downwards in the hierarchy, and more specific layouts always override more general ones. For example:</p></div>
+<div class="para"><p><tt>application.rb</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -641,17 +652,35 @@ http://www.gnu.org/software/src-highlite --> layout <span style="color: #FF0000">"main"</span>
<span style="font-style: italic"><span style="color: #9A1900">#...</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> PostsController <span style="color: #990000"><</span> ApplicationController
+</tt></pre></div></div>
+<div class="para"><p><tt>posts_controller.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-weight: bold"><span style="color: #0000FF">class</span></span> PostsController <span style="color: #990000"><</span> ApplicationController
<span style="font-style: italic"><span style="color: #9A1900"># ...</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> SpecialPostsController <span style="color: #990000"><</span> PostsController
+</tt></pre></div></div>
+<div class="para"><p><tt>special_posts_controller.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-weight: bold"><span style="color: #0000FF">class</span></span> SpecialPostsController <span style="color: #990000"><</span> PostsController
layout <span style="color: #FF0000">"special"</span>
<span style="font-style: italic"><span style="color: #9A1900"># ...</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> OldPostsController <span style="color: #990000"><</span> SpecialPostsController
+</tt></pre></div></div>
+<div class="para"><p><tt>old_posts_controller.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-weight: bold"><span style="color: #0000FF">class</span></span> OldPostsController <span style="color: #990000"><</span> SpecialPostsController
layout <span style="font-weight: bold"><span style="color: #0000FF">nil</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
@@ -693,7 +722,7 @@ In general, views will be rendered in the <tt>main</tt> layout </p>
</li>
</ul></div>
-<h4 id="_avoiding_double_render_errors">2.2.12. Avoiding Double Render Errors</h4>
+<h4 id="_avoiding_double_render_errors">2.2.13. Avoiding Double Render Errors</h4>
<div class="para"><p>Sooner or later, most Rails developers will see the error message "Can only render or redirect once per action". While this is annoying, it's relatively easy to fix. Usually it happens because of a fundamental misunderstanding of the way that <tt>render</tt> works.</p></div>
<div class="para"><p>For example, here's some code that will trigger this error:</p></div>
<div class="listingblock">
@@ -933,7 +962,7 @@ http://www.gnu.org/software/src-highlite --> by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000"><%= javascript_include_tag :all, :recursive %></span>
+<pre><tt><span style="color: #FF0000"><%= javascript_include_tag :all, :recursive =></span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #990000">%></span>
</tt></pre></div></div>
<div class="para"><p>If you're loading multiple javascript files, you can create a better user experience by combining multiple files into a single download. To make this happen in production, specify <tt>:cache ⇒ true</tt> in your <tt>javascript_include_tag</tt>:</p></div>
<div class="listingblock">
@@ -1007,7 +1036,7 @@ http://www.gnu.org/software/src-highlite --> by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000"><%= stylesheet_link_tag :all, :recursive %></span>
+<pre><tt><span style="color: #FF0000"><%= stylesheet_link_tag :all, :recursive =></span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #990000">%></span>
</tt></pre></div></div>
<div class="para"><p>If you're loading multiple CSS files, you can create a better user experience by combining multiple files into a single download. To make this happen in production, specify <tt>:cache ⇒ true</tt> in your <tt>stylesheet_link_tag</tt>:</p></div>
<div class="listingblock">
@@ -1185,26 +1214,33 @@ http://www.gnu.org/software/src-highlite --> <div class="para"><p>This would look for a partial named <tt>_link_area.html.erb</tt> and render it using the layout <tt>_graybar.html.erb</tt>. Note that layouts for partials follow the same leading-underscore naming as regular partials, and are placed in the same folder with the partial that they belong to (not in the master <tt>layouts</tt> folder).</p></div>
<h4 id="_passing_local_variables">3.4.4. Passing Local Variables</h4>
<div class="para"><p>You can also pass local variables into partials, making them even more powerful and flexible. For example, you can use this technique to reduce duplication between new and edit pages, while still keeping a bit of distinct content:</p></div>
+<div class="para"><p><tt>new.html.erb</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>new.html.erb:
-
-<span style="font-weight: bold"><span style="color: #0000FF"><h1></span></span>New zone<span style="font-weight: bold"><span style="color: #0000FF"></h1></span></span>
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF"><h1></span></span>New zone<span style="font-weight: bold"><span style="color: #0000FF"></h1></span></span>
<%= error_messages_for :zone %>
<%= render :partial => "form", :locals => { :button_label => "Create zone", :zone => @zone } %>
-
-edit.html.erb:
-
-<span style="font-weight: bold"><span style="color: #0000FF"><h1></span></span>Editing zone<span style="font-weight: bold"><span style="color: #0000FF"></h1></span></span>
+</tt></pre></div></div>
+<div class="para"><p><tt>edit.html.erb</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-weight: bold"><span style="color: #0000FF"><h1></span></span>Editing zone<span style="font-weight: bold"><span style="color: #0000FF"></h1></span></span>
<%= error_messages_for :zone %>
<%= render :partial => "form", :locals => { :button_label => "Update zone", :zone => @zone } %>
-
-_form.html.erb:
-
-<% form_for(@zone) do |f| %>
+</tt></pre></div></div>
+<div class="para"><p><tt>_form.html.erb:</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><% form_for(zone) do |f| %>
<span style="font-weight: bold"><span style="color: #0000FF"><p></span></span>
<span style="font-weight: bold"><span style="color: #0000FF"><b></span></span>Zone name<span style="font-weight: bold"><span style="color: #0000FF"></b><br</span></span> <span style="font-weight: bold"><span style="color: #0000FF">/></span></span>
<%= f.text_field :name %>
@@ -1215,7 +1251,7 @@ _form.html.erb: <% end %>
</tt></pre></div></div>
<div class="para"><p>Although the same partial will be rendered into both views, the label on the submit button is controlled by a local variable passed into the partial.</p></div>
-<div class="para"><p>Every partial also has a local variable with the same name as the partial (minus the underscore). By default, it will look for an instance variable with the same name as the partial in the parent. You can pass an object in to this local variable via the <tt>:object</tt> option:</p></div>
+<div class="para"><p>Every partial also has a local variable with the same name as the partial (minus the underscore). You can pass an object in to this local variable via the <tt>:object</tt> option:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1224,6 +1260,14 @@ http://www.gnu.org/software/src-highlite --> <pre><tt><%= render :partial => "customer", :object => @new_customer %>
</tt></pre></div></div>
<div class="para"><p>Within the <tt>customer</tt> partial, the <tt>@customer</tt> variable will refer to <tt>@new_customer</tt> from the parent view.</p></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/warning.png" alt="Warning" />
+</td>
+<td class="content">In previous versions of Rails, the default local variable would look for an instance variable with the same name as the partial in the parent. This behavior is deprecated in Rails 2.2 and will be removed in a future version.</td>
+</tr></table>
+</div>
<div class="para"><p>If you have an instance of a model to render into a partial, you can use a shorthand syntax:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
@@ -1235,19 +1279,22 @@ http://www.gnu.org/software/src-highlite --> <div class="para"><p>Assuming that the <tt>@customer</tt> instance variable contains an instance of the <tt>Customer</tt> model, this will use <tt>_customer.html.erb</tt> to render it.</p></div>
<h4 id="_rendering_collections">3.4.5. Rendering Collections</h4>
<div class="para"><p>Partials are very useful in rendering collections. When you pass a collection to a partial via the <tt>:collection</tt> option, the partial will be inserted once for each member in the collection:</p></div>
+<div class="para"><p><tt>index.html.erb</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>index.html.erb:
-
-<span style="font-weight: bold"><span style="color: #0000FF"><h1></span></span>Products<span style="font-weight: bold"><span style="color: #0000FF"></h1></span></span>
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF"><h1></span></span>Products<span style="font-weight: bold"><span style="color: #0000FF"></h1></span></span>
<%= render :partial => "product", :collection => @products %>
-
-_product.html.erb:
-
-<span style="font-weight: bold"><span style="color: #0000FF"><p></span></span>Product Name: <%= product.name %><span style="font-weight: bold"><span style="color: #0000FF"></p></span></span>
+</tt></pre></div></div>
+<div class="para"><p><tt>_product.html.erb</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-weight: bold"><span style="color: #0000FF"><p></span></span>Product Name: <%= product.name %><span style="font-weight: bold"><span style="color: #0000FF"></p></span></span>
</tt></pre></div></div>
<div class="para"><p>When a partial is called with a pluralized collection, then the individual instances of the partial have access to the member of the collection being rendered via a variable named after the partial. In this case, the partial is <tt>_product, and within the +_product</tt> partial, you can refer to <tt>product</tt> to get the instance that is being rendered. To use a custom local variable name within the partial, specify the <tt>:as</tt> option in the call to the partial:</p></div>
<div class="listingblock">
@@ -1268,38 +1315,48 @@ http://www.gnu.org/software/src-highlite --> </tt></pre></div></div>
<div class="para"><p>Rails will render the <tt>_product_ruler</tt> partial (with no data passed in to it) between each pair of <tt>_product</tt> partials.</p></div>
<div class="para"><p>There's also a shorthand syntax available for rendering collections. For example, if <tt>@products</tt> is a collection of products, you can render the collection this way:</p></div>
+<div class="para"><p><tt>index.html.erb</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>index.html.erb:
-
-<span style="font-weight: bold"><span style="color: #0000FF"><h1></span></span>Products<span style="font-weight: bold"><span style="color: #0000FF"></h1></span></span>
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF"><h1></span></span>Products<span style="font-weight: bold"><span style="color: #0000FF"></h1></span></span>
<%= render :partial => @products %>
-
-_product.html.erb:
-
-<span style="font-weight: bold"><span style="color: #0000FF"><p></span></span>Product Name: <%= product.name %><span style="font-weight: bold"><span style="color: #0000FF"></p></span></span>
+</tt></pre></div></div>
+<div class="para"><p><tt>_product.html.erb</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-weight: bold"><span style="color: #0000FF"><p></span></span>Product Name: <%= product.name %><span style="font-weight: bold"><span style="color: #0000FF"></p></span></span>
</tt></pre></div></div>
<div class="para"><p>Rails determines the name of the partial to use by looking at the model name in the collection. In fact, you can even create a heterogeneous collection and render it this way, and Rails will choose the proper partial for each member of the collection:</p></div>
+<div class="para"><p><tt>index.html.erb</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>index.html.erb:
-
-<span style="font-weight: bold"><span style="color: #0000FF"><h1></span></span>Contacts<span style="font-weight: bold"><span style="color: #0000FF"></h1></span></span>
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF"><h1></span></span>Contacts<span style="font-weight: bold"><span style="color: #0000FF"></h1></span></span>
<%= render :partial => [customer1, employee1, customer2, employee2] %>
-
-_customer.html.erb:
-
-<span style="font-weight: bold"><span style="color: #0000FF"><p></span></span>Name: <%= customer.name %><span style="font-weight: bold"><span style="color: #0000FF"></p></span></span>
-
-_employee.html.erb:
-
-<span style="font-weight: bold"><span style="color: #0000FF"><p></span></span>Name: <%= employee.name %><span style="font-weight: bold"><span style="color: #0000FF"></p></span></span>
+</tt></pre></div></div>
+<div class="para"><p><tt>_customer.html.erb</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-weight: bold"><span style="color: #0000FF"><p></span></span>Name: <%= customer.name %><span style="font-weight: bold"><span style="color: #0000FF"></p></span></span>
+</tt></pre></div></div>
+<div class="para"><p><tt>_employee.html.erb</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-weight: bold"><span style="color: #0000FF"><p></span></span>Name: <%= employee.name %><span style="font-weight: bold"><span style="color: #0000FF"></p></span></span>
</tt></pre></div></div>
<div class="para"><p>In this case, Rails will use the customer or employee partials as appropriate for each member of the collection.</p></div>
</div>
@@ -1309,6 +1366,11 @@ _employee.html.erb: <div class="ilist"><ul>
<li>
<p>
+November 1, 2008: Added <tt>:js</tt> option for <tt>render</tt> by <a href="../authors.html#mgunderloy">Mike Gunderloy</a>
+</p>
+</li>
+<li>
+<p>
October 16, 2008: Ready for publication by <a href="../authors.html#mgunderloy">Mike Gunderloy</a>
</p>
</li>
diff --git a/railties/doc/guides/html/routing_outside_in.html b/railties/doc/guides/html/routing_outside_in.html index ca8958eef9..a1f1f98cfb 100644 --- a/railties/doc/guides/html/routing_outside_in.html +++ b/railties/doc/guides/html/routing_outside_in.html @@ -1756,7 +1756,7 @@ http://www.gnu.org/software/src-highlite --> <span style="color: #990000">:</span>has_many <span style="color: #990000">=></span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>tags<span style="color: #990000">,</span> <span style="color: #990000">:</span>ratings<span style="color: #FF0000">}</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
</tt></pre></div></div>
-<div class="para"><p>As you can see, the namespaced version is much more succinct than the one that spells everything out - but it still creates the same routes. For example, you'll get <tt>admin_photos_url</tt> that expects to find an <tt>Admin::PhotosController</tt> and that matches <tt>admin/photos</tt>, and <tt>admin_photos_ratings+path</tt> that matches <tt>/admin/photos/<em>photo_id</em>/ratings</tt>, expecting to use <tt>Admin::RatingsController</tt>. Even though you're not specifying <tt>path_prefix</tt> explicitly, the routing code will calculate the appropriate <tt>path_prefix</tt> from the route nesting.</p></div>
+<div class="para"><p>As you can see, the namespaced version is much more succinct than the one that spells everything out - but it still creates the same routes. For example, you'll get <tt>admin_photos_url</tt> that expects to find an <tt>Admin::PhotosController</tt> and that matches <tt>admin/photos</tt>, and <tt>admin_photos_ratings_path</tt> that matches <tt>/admin/photos/<em>photo_id</em>/ratings</tt>, expecting to use <tt>Admin::RatingsController</tt>. Even though you're not specifying <tt>path_prefix</tt> explicitly, the routing code will calculate the appropriate <tt>path_prefix</tt> from the route nesting.</p></div>
<h3 id="_adding_more_restful_actions">3.11. Adding More RESTful Actions</h3>
<div class="para"><p>You are not limited to the seven routes that RESTful routing creates by default. If you like, you may add additional member routes (those which apply to a single instance of the resource), additional new routes (those that apply to creating a new resource), or additional collection routes (those which apply to the collection of resources as a whole).</p></div>
<h4 id="_adding_member_routes">3.11.1. Adding Member Routes</h4>
diff --git a/railties/doc/guides/html/security.html b/railties/doc/guides/html/security.html index a135d9b486..390efb5435 100644 --- a/railties/doc/guides/html/security.html +++ b/railties/doc/guides/html/security.html @@ -310,6 +310,9 @@ ul#navMain { <li> <a href="#_additional_resources">Additional resources</a> </li> + <li> + <a href="#_changelog">Changelog</a> + </li> </ol> </div> @@ -1325,6 +1328,17 @@ Another <a href="http://www.0x000000.com/">good security blog</a> with some Chea </li>
</ul></div>
</div>
+<h2 id="_changelog">10. Changelog</h2>
+<div class="sectionbody">
+<div class="para"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/7">Lighthouse ticket</a></p></div>
+<div class="ilist"><ul>
+<li>
+<p>
+November 1, 2008: First approved version by Heiko Webers
+</p>
+</li>
+</ul></div>
+</div>
</div> </div> diff --git a/railties/doc/guides/source/2_2_release_notes.txt b/railties/doc/guides/source/2_2_release_notes.txt index 8d627c9e63..2e9e9b8aa1 100644 --- a/railties/doc/guides/source/2_2_release_notes.txt +++ b/railties/doc/guides/source/2_2_release_notes.txt @@ -27,20 +27,20 @@ Along with thread safety, a lot of work has been done to make Rails work well wi The internal documentation of Rails, in the form of code comments, has been improved in numerous places. In addition, the link:http://guides.rubyonrails.org/[Ruby on Rails Guides] project is the definitive source for information on major Rails components. In its first official release, the Guides page includes: -* Getting Started with Rails -* Rails Database Migrations -* Active Record Associations -* Active Record Finders -* Layouts and Rendering in Rails -* Action View Form Helpers -* Rails Routing from the Outside In -* Basics of Action Controller -* Rails Caching -* Testing Rails Applications -* Securing Rails Applications -* Debugging Rails Applications -* Benchmarking and Profiling Rails Applications -* The Basics of Creating Rails Plugins +* link:http://guides.rubyonrails.org/getting_started_with_rails.html[Getting Started with Rails] +* link:http://guides.rubyonrails.org/migrations.html[Rails Database Migrations] +* link:http://guides.rubyonrails.org/association_basics.html[Active Record Associations] +* link:http://guides.rubyonrails.org/finders.html[Active Record Finders] +* link:http://guides.rubyonrails.org/layouts_and_rendering.html[Layouts and Rendering in Rails] +* link:http://guides.rubyonrails.org/form_helpers.html[Action View Form Helpers] +* link:http://guides.rubyonrails.org/routing_outside_in.html[Rails Routing from the Outside In] +* link:http://guides.rubyonrails.org/actioncontroller_basics.html[Basics of Action Controller] +* link:http://guides.rubyonrails.org/caching_with_rails.html[Rails Caching] +* link:http://guides.rubyonrails.org/testing_rails_applications.html[Testing Rails Applications] +* link:http://guides.rubyonrails.org/security.html[Securing Rails Applications] +* link:http://guides.rubyonrails.org/debugging_rails_applications.html[Debugging Rails Applications] +* link:http://guides.rubyonrails.org/benchmarking_and_profiling.html[Benchmarking and Profiling Rails Applications] +* link:http://guides.rubyonrails.org/creating_plugins.html[The Basics of Creating Rails Plugins] All told, the Guides provide tens of thousands of words of guidance for beginning and intermediate Rails developers. @@ -167,7 +167,7 @@ Product.all(:joins => :photos, :conditions => { :photos => { :copyright => false Two new sets of methods have been added to Active Record's dynamic finders family. -==== find_last_by_<attributes> +==== +find_last_by_<attribute>+ The +find_last_by_<attribute>+ method is equivalent to +Model.last(:conditions => {:attribute => value})+ @@ -179,9 +179,9 @@ User.find_last_by_city('London') * Lead Contributor: link:http://www.workingwithrails.com/person/9147-emilio-tagua[Emilio Tagua] -==== find_by_<attributes>! +==== +find_by_<attribute>!+ -The new bang! version of +find_by_<attribute>! is equivalent to +Model.first(:conditions => {:attribute => value}) || raise ActiveRecord::RecordNotFound+ Instead of returning +nil+ if it can't find a matching record, this method will raise an exception if it cannot find a match. +The new bang! version of +find_by_<attribute>!+ is equivalent to +Model.first(:conditions => {:attribute => value}) || raise ActiveRecord::RecordNotFound+ Instead of returning +nil+ if it can't find a matching record, this method will raise an exception if it cannot find a match. [source, ruby] ------------------------------------------------------- @@ -257,11 +257,13 @@ Action Controller now offers good support for HTTP conditional GET requests, as * The HTTP Accept header is disabled by default now. You should prefer the use of formatted URLs (such as +/customers/1.xml+) to indicate the format that you want. If you need the Accept headers, you can turn them back on with +config.action_controller.user_accept_header = true+. * Benchmarking numbers are now reported in milliseconds rather than tiny fractions of seconds * Rails now supports HTTP-only cookies (and uses them for sessions), which help mitigate some cross-site scripting risks in newer browsers. +* +redirect_to+ now fully supports URI schemes (so, for example, you can redirect to a svn+ssh: URI). +* +render+ now supports a +:js+ option to render plain vanilla javascript with the right mime type. == Action View * +javascript_include_tag+ and +stylesheet_link_tag+ support a new +:recursive+ option to be used along with +:all+, so that you can load an entire tree of files with a single line of code. -* The included Prototype javascript library has been upgraded to version 1.6.0.2. +* The included Prototype javascript library has been upgraded to version 1.6.0.3. * +RJS#page.reload+ to reload the browser's current location via javascript * The +atom_feed+ helper now takes an +:instruct+ option to let you insert XML processing instructions. @@ -323,23 +325,23 @@ If you delegate behavior from one class to another, you can now specify a prefix [source, ruby] ------------------------------------------------------- -class Vendor << ActiveRecord::Base +class Vendor < ActiveRecord::Base has_one :account delegate :email, :password, :to => :account, :prefix => true end ------------------------------------------------------- -This will produce delegated methods +vendor.account_email+ and +vendor.account_password+. You can also specify a custom prefix: +This will produce delegated methods +vendor#account_email+ and +vendor#account_password+. You can also specify a custom prefix: [source, ruby] ------------------------------------------------------- -class Vendor << ActiveRecord::Base +class Vendor < ActiveRecord::Base has_one :account delegate :email, :password, :to => :account, :prefix => :owner end ------------------------------------------------------- -This will produce delegated methods +vendor.owner_email+ and +vendor.owner_password+. +This will produce delegated methods +vendor#owner_email+ and +vendor#owner_password+. Lead Contributor: link:http://workingwithrails.com/person/5830-daniel-schierbeck[Daniel Schierbeck] @@ -353,6 +355,7 @@ Lead Contributor: link:http://workingwithrails.com/person/5830-daniel-schierbeck * +Inflector#parameterize+ produces a URL-ready version of its input, for use in +to_param+. * +Time#advance+ recognizes fractional days and weeks, so you can do +1.7.weeks.ago+, +1.5.hours.since+, and so on. * The included TzInfo library has been upgraded to version 0.3.11. +* +ActiveSuport::StringInquirer+ gives you a pretty way to test for equality in strings: +ActiveSupport::StringInquirer.new("abc").abc? => true+ == Railties @@ -370,11 +373,12 @@ To avoid deployment issues and make Rails applications more self-contained, it's * +rake gems:build+ to build any missing native extensions * +rake gems:refresh_specs+ to bring vendored gems created with Rails 2.1 into alignment with the Rails 2.2 way of storing them -You can unpack or install a single gem by specifying +GEM=_gem_name+ on the command line. +You can unpack or install a single gem by specifying +GEM=_gem_name_+ on the command line. * Lead Contributor: link:http://github.com/al2o3cr[Matt Jones] * More information: - link:http://ryandaigle.com/articles/2008/4/1/what-s-new-in-edge-rails-gem-dependencies[What's New in Edge Rails: Gem Dependencies] + - link:http://afreshcup.com/2008/10/25/rails-212-and-22rc1-update-your-rubygems/[Rails 2.1.2 and 2.2RC1: Update Your RubyGems] === Other Railties Changes @@ -383,7 +387,7 @@ You can unpack or install a single gem by specifying +GEM=_gem_name+ on the comm * +script/console+ now supports a +--debugger+ option * Instructions for setting up a continuous integration server to build Rails itself are included in the Rails source * +rake notes:custom ANNOTATION=MYFLAG+ lets you list out custom annotations. -* Wrapped +Rails.env+ in +StringQuestioneer+ so you can do +Rails.env.development?+ +* Wrapped +Rails.env+ in +StringInquirer+ so you can do +Rails.env.development?+ * +script/generate+ works without deprecation warnings when RubyGems 1.3.0 is present == Deprecated diff --git a/railties/doc/guides/source/actioncontroller_basics/filters.txt b/railties/doc/guides/source/actioncontroller_basics/filters.txt index a7b8d9727f..a6f688d144 100644 --- a/railties/doc/guides/source/actioncontroller_basics/filters.txt +++ b/railties/doc/guides/source/actioncontroller_basics/filters.txt @@ -55,8 +55,6 @@ Now, the LoginsController's "new" and "create" actions will work as before witho In addition to the before filters, you can run filters after an action has run or both before and after. The after filter is similar to the before filter, but because the action has already been run it has access to the response data that's about to be sent to the client. Obviously, after filters can not stop the action from running. Around filters are responsible for running the action, but they can choose not to, which is the around filter's way of stopping it. -TODO: Find a real example for an around filter - [source, ruby] --------------------------------- # Example taken from the Rails API filter documentation: diff --git a/railties/doc/guides/source/active_record_basics.txt b/railties/doc/guides/source/active_record_basics.txt index e0542ff62e..15fc544f25 100644 --- a/railties/doc/guides/source/active_record_basics.txt +++ b/railties/doc/guides/source/active_record_basics.txt @@ -1,7 +1,7 @@ Active Record Basics ==================== -Active Record is a design pattern that mitigates the mind-numbing mental gymnastics often needed to get your application to communicate with a database. This guide explains in detail how This guide uses a mix of real-world examples, metaphors and detailed explanations of the actual Rails source code to help you make the most of AcitveRecord. +Active Record is a design pattern that mitigates the mind-numbing mental gymnastics often needed to get your application to communicate with a database. This guide uses a mix of real-world examples, metaphors and detailed explanations of the actual Rails source code to help you make the most of AcitveRecord. After reading this guide readers should have a strong grasp of the Active Record pattern and how it can be used with or without Rails. Hopefully, some of the philosophical and theoretical intentions discussed here will also make them a stronger and better developer. @@ -178,4 +178,4 @@ Rails has a reputation of being a zero-config framework which means that it aim * (6) before_create * (-) create * (7) after_create - * (8) after_save
\ No newline at end of file + * (8) after_save diff --git a/railties/doc/guides/source/association_basics.txt b/railties/doc/guides/source/association_basics.txt index 695b834652..5ba616642b 100644 --- a/railties/doc/guides/source/association_basics.txt +++ b/railties/doc/guides/source/association_basics.txt @@ -359,7 +359,7 @@ class Employee < ActiveRecord::Base end ------------------------------------------------------- -With this setup, you can retrieve +@employee.subordinates+ and +@employee.managers+. +With this setup, you can retrieve +@employee.subordinates+ and +@employee.manager+. == Tips, Tricks, and Warnings @@ -765,6 +765,8 @@ If you set the +:readonly+ option to +true+, then the associated object will be The +:select+ option lets you override the SQL +SELECT+ clause that is used to retrieve data about the associated object. By default, Rails retrieves all columns. +TIP: If you set the +:select+ option on a +belongs_to+ association, you should also set the +foreign_key+ option to guarantee the correct results. + ===== +:validate+ If you set the +:validate+ option to +true+, then associated objects will be validated whenever you save this object. By default, this is +false+: associated objects will not be validated when this object is saved. diff --git a/railties/doc/guides/source/finders.txt b/railties/doc/guides/source/finders.txt index a86191aaf4..945b527e1d 100644 --- a/railties/doc/guides/source/finders.txt +++ b/railties/doc/guides/source/finders.txt @@ -1,25 +1,28 @@ Rails Finders ============= -This guide is all about the `find` method defined in ActiveRecord::Base, finding on associations, and associated goodness such as named scopes. You will learn how to be a find master. +This guide is all about the +find+ method defined in +ActiveRecord::Base+, finding on associations, and associated goodness such as named scopes. You will learn how to be a find master. == In the beginning... In the beginning there was SQL. SQL looked like this: [source,sql] +------------------------------------------------------- SELECT * FROM clients SELECT * FROM clients WHERE id = '1' SELECT * FROM clients LIMIT 0,1 SELECT * FROM clients ORDER BY id DESC LIMIT 0,1 +------------------------------------------------------- -In Rails you don't usually have to type SQL (unlike other languages) because ActiveRecord is there to help you find your records. +In Rails (unlike some other frameworks) you don't usually have to type SQL because Active Record is there to help you find your records. -== Our Models +== The Sample Models -For this guide we have the following models: +This guide demonstrates finding using the following models: [source,ruby] +------------------------------------------------------- class Client < ActiveRecord::Base has_one :address has_one :mailing_address @@ -27,424 +30,598 @@ class Client < ActiveRecord::Base has_and_belongs_to_many :roles end -[source,ruby] class Address < ActiveRecord::Base belongs_to :client end -[source,ruby] class MailingAddress < Address end -[source,ruby] class Order < ActiveRecord::Base belongs_to :client, :counter_cache => true end -[source,ruby] class Role < ActiveRecord::Base has_and_belongs_to_many :clients end +------------------------------------------------------- == Database Agnostic -ActiveRecord 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 ActiveRecord method format will always be the same. +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. == IDs, First, Last and All -ActiveRecord::Base has methods defined on it to make interacting with your database and the tables within it much, much easier: find. 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 Client.find(1) which would execute this query on your database: ++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 +find+. 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 +Client.find(1)+ which would execute this query on your database: [source, sql] -SELECT * FROM `clients` WHERE (`clients`.`id` = 1) -NOTE: Please be aware that because this is a standard table created from a migration in Rails that the primary key is defaulted to 'id'. If you have specified a different primary key in your migrations, this is what Rails will find on when you call the find method, not the id column. +------------------------------------------------------- +SELECT * FROM +clients+ WHERE (+clients+.+id+ = 1) +------------------------------------------------------- + +NOTE: Because this is a standard table created from a migration in Rail, the primary key is defaulted to 'id'. If you have specified a different primary key in your migrations, this is what Rails will find on when you call the find method, not the id column. -If you wanted to find clients with id 1 or 2, you call `Client.find([1,2])` or `Client.find(1,2)` and then this will be executed as: +If you wanted to find clients with id 1 or 2, you call +Client.find([1,2])+ or +Client.find(1,2)+ and then this will be executed as: [source, sql] -SELECT * FROM `clients` WHERE (`clients`.`id` IN (1,2)) +------------------------------------------------------- +SELECT * FROM +clients+ WHERE (+clients+.+id+ IN (1,2)) +------------------------------------------------------- + [source,txt] +------------------------------------------------------- >> Client.find(1,2) -=> [#<Client id: 1, name: => "Ryan", locked: false, orders_count: 2, created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50">, #<Client id: 2, name: => "Michael", locked: false, orders_count: 3, created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40">] +=> [#<Client id: 1, name: => "Ryan", locked: false, orders_count: 2, + created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50">, + #<Client id: 2, name: => "Michael", locked: false, orders_count: 3, + created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40">] +------------------------------------------------------- -Note that if you pass in a list of numbers that the result will be returned as an array, not an object of Client. +Note that if you pass in a list of numbers that the result will be returned as an array, not as a single +Client+ object. -If you wanted to find the first client you would simply type `Client.find(:first)` and that would find the first client created in your clients table: +If you wanted to find the first client you would simply type +Client.first+ and that would find the first client created in your clients table: [source,txt] ->> Client.find(:first) -=> #<Client id: 1, name: => "Ryan", locked: false, orders_count: 2, created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50"> -If you were running script/server you may see the following output: +------------------------------------------------------- +>> Client.first +=> #<Client id: 1, name: => "Ryan", locked: false, orders_count: 2, + created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50"> +------------------------------------------------------- + +If you were running script/server you might see the following output: [source,sql] +------------------------------------------------------- SELECT * FROM clients LIMIT 1 +------------------------------------------------------- Indicating the query that Rails has performed on your database. -To find the last client you would simply type `Client.find(:last)` and that would find the last client created in your clients table: +To find the last client you would simply type +Client.find(:last)+ and that would find the last client created in your clients table: [source,txt] +------------------------------------------------------- >> Client.find(:last) -=> #<Client id: 2, name: => "Michael", locked: false, orders_count: 3, created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40"> +=> #<Client id: 2, name: => "Michael", locked: false, orders_count: 3, + created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40"> +------------------------------------------------------- [source,sql] +------------------------------------------------------- SELECT * FROM clients ORDER BY clients.id DESC LIMIT 1 +------------------------------------------------------- -To find all the clients you would simply type `Client.find(:all)` and that would find all the clients in your clients table: +To find all the clients you would simply type +Client.all+ and that would find all the clients in your clients table: [source,txt] ->> Client.find(:all) -=> [#<Client id: 1, name: => "Ryan", locked: false, orders_count: 2, created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50">, #<Client id: 2, name: => "Michael", locked: false, orders_count: 3, created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40">] +------------------------------------------------------- +>> Client.all +=> [#<Client id: 1, name: => "Ryan", locked: false, orders_count: 2, + created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50">, + #<Client id: 2, name: => "Michael", locked: false, orders_count: 3, + created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40">] +------------------------------------------------------- -Alternatively to calling Client.find(:first)/`Client.find(:last)`/`Client.find(:all)`, you could use the class method of `Client.first`/`Client.last`/`Client.all` instead. `Client.first`, `Client.last` and `Client.all` just call their longer counterparts. +As alternatives to calling +Client.first+, +Client.last+, and +Client.all+, you can use the class methods +Client.first+, +Client.last+, and +Client.all+ instead. +Client.first+, +Client.last+ and +Client.all+ just call their longer counterparts: +Client.find(:first)+, +Client.find(:last)+ and +Client.find(:all)+ respectively. -Be aware that `Client.first`/`Client.find(:first)` and `Client.last`/`Client.find(:last)` will both return a single object, where as `Client.all`/`Client.find(:all)` will return an array of Client objects, just as passing in an array of ids to find will do also. +Be aware that +Client.first+/+Client.find(:first)+ and +Client.last+/+Client.find(:last)+ will both return a single object, where as +Client.all+/+Client.find(:all)+ will return an array of Client objects, just as passing in an array of ids to find will do also. == Conditions -If you'd like to add conditions to your find, you could just specify them in there, just like `Client.find(:first, :conditions => "orders_count = '2'")`. Now what if that number could vary, say as a parameter from somewhere, or perhaps from the user's level status somewhere? The find then becomes something like `Client.find(:first, :conditions => ["orders_count = ?", params[:orders]])`. ActiveRecord 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 `Client.find(:first, :conditions => ["orders_count = ? AND locked = ?", params[:orders], false])`. In this example, the first question mark will be replaced with the value in params orders and the second will be replaced with true and this will find the first record in the table that has '2' as its value for the orders_count field and 'false' for its locked field. +=== Pure String Conditions === + +If you'd like to add conditions to your find, you could just specify them in there, just like +Client.first(:conditions => "orders_count = '2'")+. This will find all clients where the +orders_count+ field's value is 2. + +=== Array Conditions === + + Now what if that number could vary, say as a parameter from somewhere, or perhaps from the user's level status somewhere? The find then becomes something like +Client.first(:conditions => ["orders_count = ?", params[:orders]])+. Active Record will go through the first element in the conditions value and any additional elements will replace the question marks (?) in the first element. If you want to specify two conditions, you can do it like +Client.first(:conditions => ["orders_count = ? AND locked = ?", params[:orders], false])+. In this example, the first question mark will be replaced with the value in params orders and the second will be replaced with true and this will find the first record in the table that has '2' as its value for the orders_count field and 'false' for its locked field. The reason for doing code like: [source, ruby] -`Client.find(:first, :conditions => ["orders_count = ?", params[:orders]])` +------------------------------------------------------- ++Client.first(:conditions => ["orders_count = ?", params[:orders]])+ +------------------------------------------------------- instead of: -`Client.find(:first, :conditions => "orders_count = #{params[:orders]}")` +------------------------------------------------------- ++Client.first(:conditions => "orders_count = #{params[:orders]}")+ +------------------------------------------------------- -is because of parameter safety. Putting the variable directly into the conditions string will parse the variable *as-is*. This means that it will be an unescaped variable directly from a user who may have malicious intent. If you do this, you put your entire database at risk because once a user finds out he or she can exploit your database they can do just about anything to it. Never ever put your parameters directly inside the conditions string. +is because of parameter safety. Putting the variable directly into the conditions string will pass the variable to the database *as-is*. This means that it will be an unescaped variable directly from a user who may have malicious intent. If you do this, you put your entire database at risk because once a user finds out he or she can exploit your database they can do just about anything to it. Never ever put your parameters directly inside the conditions string. -If you're looking for a range inside of a table for example users created in a certain timeframe you can use the conditions option coupled with the IN sql statement for this. If we had two dates coming in from a controller we could do something like this to look for a range: +TIP: For more information on the dangers of SQL injection, see the link:../security.html#_sql_injection[Ruby on Rails Security Guide]. + +If you're looking for a range inside of a table (for example, users created in a certain timeframe) you can use the conditions option coupled with the IN sql statement for this. If you had two dates coming in from a controller you could do something like this to look for a range: [source, ruby] -Client.find(:all, :conditions => ["created_at IN (?)", (params[:start_date].to_date)..(params[:end_date].to_date)]) +------------------------------------------------------- +Client.all(:conditions => ["created_at IN (?)", + (params[:start_date].to_date)..(params[:end_date].to_date)]) +------------------------------------------------------- This would generate the proper query which is great for small ranges but not so good for larger ranges. For example if you pass in a range of date objects spanning a year that's 365 (or possibly 366, depending on the year) strings it will attempt to match your field against. [source, sql] -SELECT * FROM `users` WHERE (created_at IN ('2007-12-31','2008-01-01','2008-01-02','2008-01-03','2008-01-04','2008-01-05','2008-01-06','2008-01-07','2008-01-08','2008-01-09','2008-01-10','2008-01-11','2008-01-12','2008-01-13','2008-01-14','2008-01-15','2008-01-16','2008-01-17','2008-01-18','2008-01-19','2008-01-20','2008-01-21','2008-01-22','2008-01-23',... -2008-12-15','2008-12-16','2008-12-17','2008-12-18','2008-12-19','2008-12-20','2008-12-21','2008-12-22','2008-12-23','2008-12-24','2008-12-25','2008-12-26','2008-12-27','2008-12-28','2008-12-29','2008-12-30','2008-12-31')) - +------------------------------------------------------- +SELECT * FROM +users+ WHERE (created_at IN + ('2007-12-31','2008-01-01','2008-01-02','2008-01-03','2008-01-04','2008-01-05', + '2008-01-06','2008-01-07','2008-01-08','2008-01-09','2008-01-10','2008-01-11', + '2008-01-12','2008-01-13','2008-01-14','2008-01-15','2008-01-16','2008-01-17', + '2008-01-18','2008-01-19','2008-01-20','2008-01-21','2008-01-22','2008-01-23',... + ‘2008-12-15','2008-12-16','2008-12-17','2008-12-18','2008-12-19','2008-12-20', + '2008-12-21','2008-12-22','2008-12-23','2008-12-24','2008-12-25','2008-12-26', + '2008-12-27','2008-12-28','2008-12-29','2008-12-30','2008-12-31')) +------------------------------------------------------- Things can get *really* messy if you pass in time objects as it will attempt to compare your field to *every second* in that range: [source, ruby] -Client.find(:all, :conditions => ["created_at IN (?)", (params[:start_date].to_date.to_time)..(params[:end_date].to_date.to_time)]) +------------------------------------------------------- +Client.all(:conditions => ["created_at IN (?)", + (params[:start_date].to_date.to_time)..(params[:end_date].to_date.to_time)]) +------------------------------------------------------- [source, sql] -SELECT * FROM `users` WHERE (created_at IN ('2007-12-01 00:00:00', '2007-12-01 00:00:01' ... '2007-12-01 23:59:59', '2007-12-02 00:00:00')) +------------------------------------------------------- +SELECT * FROM +users+ WHERE (created_at IN + ('2007-12-01 00:00:00', '2007-12-01 00:00:01' ... + '2007-12-01 23:59:59', '2007-12-02 00:00:00')) +------------------------------------------------------- This could possibly cause your database server to raise an unexpected error, for example MySQL will throw back this error: [source, txt] -Got a packet bigger than 'max_allowed_packet' bytes: <query> +------------------------------------------------------- +Got a packet bigger than 'max_allowed_packet' bytes: _query_ +------------------------------------------------------- -Where <query> is the actual query used to get that error. +Where _query_ is the actual query used to get that error. In this example it would be better to use greater-than and less-than operators in SQL, like so: [source, ruby] -Client.find(:all, :condtions => ["created_at > ? AND created_at < ?", params[:start_date], params[:end_date]]) +------------------------------------------------------- +Client.all(:conditions => + ["created_at > ? AND created_at < ?", params[:start_date], params[:end_date]]) +------------------------------------------------------- You can also use the greater-than-or-equal-to and less-than-or-equal-to like this: [source, ruby] -Client.find(:all, :condtions => ["created_at >= ? AND created_at <= ?", params[:start_date], params[:end_date]]) +------------------------------------------------------- +Client.all(:conditions => + ["created_at >= ? AND created_at <= ?", params[:start_date], params[:end_date]]) +------------------------------------------------------- Just like in Ruby. +=== Hash Conditions === + +Similar to the array style of params you can also specify keys in your conditions: + +[source, ruby] +------------------------------------------------------- +Client.all(:conditions => + ["created_at >= :start_date AND created_at <= :end_date", { :start_date => params[:start_date], :end_date => params[:end_date] }]) +------------------------------------------------------- + +This makes for clearer readability if you have a large number of variable conditions. + == Ordering -If you're getting a set of records and want to force an order, you can use `Client.find(:all, :order => "created_at")` which by default will sort the records by ascending order. If you'd like to order it in descending order, just tell it to do that using `Client.find(:all, :order => "created_at desc")` +If you're getting a set of records and want to force an order, you can use +Client.all(:order => "created_at")+ which by default will sort the records by ascending order. If you'd like to order it in descending order, just tell it to do that using +Client.all(:order => "created_at desc")+ == Selecting Certain Fields -To select certain fields, you can use the select option like this: `Client.find(:first, :select => "viewable_by, locked")`. This select option does not use an array of fields, but rather requires you to type SQL-like code. The above code will execute `SELECT viewable_by, locked FROM clients LIMIT 0,1` on your database. +To select certain fields, you can use the select option like this: +Client.first(:select => "viewable_by, locked")+. This select option does not use an array of fields, but rather requires you to type SQL-like code. The above code will execute +SELECT viewable_by, locked FROM clients LIMIT 0,1+ on your database. == Limit & Offset If you want to limit the amount of records to a certain subset of all the records retreived you usually use limit for this, sometimes coupled with offset. Limit is the maximum number of records that will be retreived 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: [source, ruby] -Client.find(:all, :limit => 5) +------------------------------------------------------- +Client.all(:limit => 5) +------------------------------------------------------- -This code will return a maximum of 5 clients and because we've specified no offset it will return the first 5 clients in the table. The SQL it executes will look like this: +This code will return a maximum of 5 clients and because it specifies no offset it will return the first 5 clients in the table. The SQL it executes will look like this: [source,sql] +------------------------------------------------------- SELECT * FROM clients LIMIT 5 +------------------------------------------------------- [source, ruby] -Client.find(:all, :limit => 5, :offset => 5) +------------------------------------------------------- +Client.all(:limit => 5, :offset => 5) +------------------------------------------------------- -This code will return a maximum of 5 clients and because we have specified an offset this time, it will return these records starting from the 5th client in the clients table. The SQL looks like: +This code will return a maximum of 5 clients and because it specifies an offset this time, it will return these records starting from the 5th client in the clients table. The SQL looks like: [source,sql] +------------------------------------------------------- SELECT * FROM clients LIMIT 5, 5 +------------------------------------------------------- == Group -The group option for find is useful, for example, if you want to find a collection of the dates orders were created on. We could use the option in this context: +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: [source, ruby] -Order.find(:all, :group => "date(created_at)", :order => "created_at") +------------------------------------------------------- +Order.all(:group => "date(created_at)", :order => "created_at") +------------------------------------------------------- -And this will give us a single Order object for each date that we have orders in our database. +And this will give you a single +Order+ object for each date where there are orders in the database. The SQL that would be executed would be something like this: [source, sql] -SELECT * FROM `orders` GROUP BY date(created_at) +------------------------------------------------------- +SELECT * FROM +orders+ GROUP BY date(created_at) +------------------------------------------------------- == Read Only -Readonly is a find 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` error. To set this option, specify it like this: +Readonly is a find 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 +Active Record::ReadOnlyRecord+ error. To set this option, specify it like this: [source, ruby] -Client.find(:first, :readonly => true) +------------------------------------------------------- +Client.first(:readonly => true) +------------------------------------------------------- -If you assign this record to a variable `client` calling the following code will raise an ActiveRecord::ReadOnlyRecord: +If you assign this record to a variable +client+, calling the following code will raise an ActiveRecord::ReadOnlyRecord: [source, ruby] -client = Client.find(:first, :readonly => true) +------------------------------------------------------- +client = Client.first(:readonly => true) client.locked = false client.save +------------------------------------------------------- == Lock -If you're wanting to stop race conditions for a specific record, say for example you're incrementing a single field for a record you can use the lock option to ensure that the record is updated correctly. It's recommended this be used inside a transaction. +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. [source, ruby] +------------------------------------------------------- Topic.transaction do t = Topic.find(params[:id], :lock => true) t.increment!(:views) end +------------------------------------------------------- == Making It All Work Together -You can chain these options together in no particular order as ActiveRecord will write the correct SQL for you. If you specify two instances of the same options inside the find statement ActiveRecord will use the latter. +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 find statement ActiveRecord will use the latter. == Eager Loading -Eager loading is loading associated records along with any number of records in as few queries as possible. Lets say for example if we wanted to load all the addresses associated with all the clients all in the same query we would use `Client.find(:all, :include => :address)`. If we wanted to include both the address and mailing address for the client we would use `Client.find(:all), :include => [:address, :mailing_address]). Inclue 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: +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 +Client.all(:include => :address)+. If you wanted to include both the address and mailing address for the client you would use +Client.find(:all), :include => [:address, :mailing_address]). 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: [source, sql] +------------------------------------------------------- Client Load (0.000383) SELECT * FROM clients -Address Load (0.119770) SELECT addresses.* FROM addresses WHERE (addresses.client_id IN (13,14)) -MailingAddress Load (0.001985) SELECT mailing_addresses.* FROM mailing_addresses WHERE (mailing_addresses.client_id IN (13,14)) +Address Load (0.119770) SELECT addresses.* FROM addresses + WHERE (addresses.client_id IN (13,14)) +MailingAddress Load (0.001985) SELECT mailing_addresses.* FROM + mailing_addresses WHERE (mailing_addresses.client_id IN (13,14)) +------------------------------------------------------- -The numbers `13` and `14` in the above SQL are the ids of the clients gathered from the `Client.find(:all)` query. Rails will then run a query to gather all the addresses and mailing addresses that have a client_id of 13 or 14. Although this is done in 3 queries, this is more efficient than not eager loading because without eager loading it would run a query for every time you called `address` or `mailing_address` on one of the objects in the clients array, which may lead to performance issues if you're loading a large number of records at once. +The numbers +13+ and +14+ in the above SQL are the ids of the clients gathered from the +Client.all+ query. Rails will then run a query to gather all the addresses and mailing addresses that have a client_id of 13 or 14. Although this is done in 3 queries, this is more efficient than not eager loading because without eager loading it would run a query for every time you called +address+ or +mailing_address+ on one of the objects in the clients array, which may lead to performance issues if you're loading a large number of records at once. -An alternative (and more efficient) way to do eager loading is to use the joins option. For example if we wanted to get all the addresses for a client we would do `Client.find(:all, :joins => :address)` and if we wanted to find the address and mailing address for that client we would do `Client.find(:all, :joins => [:address, :mailing_address])`. This is more efficient because it does all the SQL in one query, as shown by this example: +If you wanted to get all the addresses for a client in the same query you would do +Client.all(:joins => :address)+ and you wanted to find the address and mailing address for that client you would do +Client.all(:joins => [:address, :mailing_address])+. This is more efficient because it does all the SQL in one query, as shown by this example: [source, sql] -`Client Load (0.000455) SELECT clients.* FROM clients INNER JOIN addresses ON addresses.client_id = client.id INNER JOIN mailing_addresses ON mailing_addresses.client_id = client.id +------------------------------------------------------- ++Client Load (0.000455) SELECT clients.* FROM clients INNER JOIN addresses + ON addresses.client_id = client.id INNER JOIN mailing_addresses ON + mailing_addresses.client_id = client.id +------------------------------------------------------- -This query is more efficent, but there's a gotcha. If you have a client who does not have an address or a mailing address they will not be returned in this query at all. If you have any association as an optional association, you may want to use include rather than joins. - -When using eager loading you can specify conditions for the columns of the tables inside the eager loading to get back a smaller subset. If, for example, you want to find a client and all their orders within the last two weeks you could use eager loading with conditions for this: +This query is more efficent, but there's a gotcha: if you have a client who does not have an address or a mailing address they will not be returned in this query at all. If you have any association as an optional association, you may want to use include rather than joins. Alternatively, you can use a SQL join clause to specify exactly the join you need (Rails always assumes an inner join): [source, ruby] +------------------------------------------------------- +Client.all(:joins => “LEFT OUTER JOIN addresses ON + client.id = addresses.client_id LEFT OUTER JOIN mailing_addresses ON + client.id = mailing_addresses.client_id”) +------------------------------------------------------- -Client.find(:first, :include => "orders", :conditions => ["orders.created_at >= ? AND orders.created_at <= ?", Time.now - 2.weeks, Time.now]) +When using eager loading you can specify conditions for the columns of the tables inside the eager loading to get back a smaller subset. If, for example, you want to find a client and all their orders within the last two weeks you could use eager loading with conditions for this: -[source] +[source, ruby] +------------------------------------------------------- +Client.first(:include => "orders", :conditions => + ["orders.created_at >= ? AND orders.created_at <= ?", Time.now - 2.weeks, Time.now]) +------------------------------------------------------- == Dynamic finders -With every field (also known as an attribute) you define in your table, ActiveRecord provides finder methods for these. If you have a field called `name` on your Client model for example, you get `find_by_name` and `find_all_by_name` for free from ActiveRecord. If you have also have a `locked` field on the client model, you also get `find_by_locked` and `find_all_by_locked`. If you want to find both by name and locked, you can chain these finders together by simply typing and between the fields for example `Client.find_by_name_and_locked('Ryan', true)`. These finders are an excellent alternative to using the conditions option, mainly because it's shorter to type `find_by_name(params[:name])` than it is to type `find(:first, :conditions => ["name = ?", params[:name]])`. +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 +name+ on your Client model for example, you get +find_by_name+ and +find_all_by_name+ for free from Active Record. If you have also have a +locked+ field on the client model, you also get +find_by_locked+ and +find_all_by_locked+. If you want to find both by name and locked, you can chain these finders together by simply typing +and+ between the fields for example +Client.find_by_name_and_locked('Ryan', true)+. These finders are an excellent alternative to using the conditions option, mainly because it's shorter to type +find_by_name(params[:name])+ than it is to type +first(:conditions => ["name = ?", params[:name]])+. -There's another set of dynamic finders that let you find or create/initialize objects if they aren't find. These work in a similar fashion to the other finders and can be used like `find_or_create_by_name(params[:name])`. Using this will firstly perform a find and then create if the find returns nil, the SQL looks like this for `Client.find_or_create_by_name('Ryan')`: +There's another set of dynamic finders that let you find or create/initialize objects if they aren't find. These work in a similar fashion to the other finders and can be used like +find_or_create_by_name(params[:name])+. Using this will firstly perform a find and then create if the find returns nil. The SQL looks like this for +Client.find_or_create_by_name('Ryan')+: [source,sql] -SELECT * FROM `clients` WHERE (`clients`.`name` = 'Ryan') LIMIT 1 +------------------------------------------------------- +SELECT * FROM +clients+ WHERE (+clients+.+name+ = 'Ryan') LIMIT 1 BEGIN -INSERT INTO `clients` (`name`, `updated_at`, `created_at`, `orders_count`, `locked`) VALUES('Ryan', '2008-09-28 15:39:12', '2008-09-28 15:39:12', '0', '0') +INSERT INTO +clients+ (+name+, +updated_at+, +created_at+, +orders_count+, +locked+) + VALUES('Ryan', '2008-09-28 15:39:12', '2008-09-28 15:39:12', '0', '0') COMMIT +------------------------------------------------------- -`find_or_create`'s sibling, find_or_initialize, will find an object and if it does not exist will call `new` with the parameters you passed in. For example: ++find_or_create+'s sibling, +find_or_initialize+, will find an object and if it does not exist will call +new+ with the parameters you passed in. For example: [source, ruby] +------------------------------------------------------- client = Client.find_or_initialize_by_name('Ryan') +------------------------------------------------------- -will either assign an existing client object with the name 'Ryan' to the client local variable, or initialize new object similar to calling `Client.new(:name => 'Ryan')`. From here, you can modify other fields in client by calling the attribute setters on it: `client.locked = true` and when you want to write it to the database just call `save` on it. +will either assign an existing client object with the name 'Ryan' to the client local variable, or initialize new object similar to calling +Client.new(:name => 'Ryan')+. From here, you can modify other fields in client by calling the attribute setters on it: +client.locked = true+ and when you want to write it to the database just call +save+ on it. == Finding By SQL -If you'd like to use your own SQL to find records a table you can use `find_by_sql`. `find_by_sql` will return an array of objects even if it only returns a single record in it's call to the database. For example you could run this query: +If you'd like to use your own SQL to find records a table you can use +find_by_sql+. The +find_by_sql+ method will return an array of objects even if it only returns a single record in it's call to the database. For example you could run this query: [source, ruby] +------------------------------------------------------- Client.find_by_sql("SELECT * FROM clients INNER JOIN orders ON clients.id = orders.client_id ORDER clients.created_at desc") +------------------------------------------------------- + ++find_by_sql+ provides you with a simple way of making custom calls to the database and retreiving instantiated objects. -`find_by_sql` provides you with a simple way of making custom calls to the database and converting those to objects. +== +select_all+ == + ++find_by_sql+ has a close relative called +select_all+. +select_all+ will retreive objects from the database using custom SQL just like +find_by_sql+ but will not instantiate them. Instead, you will get an array of hashes where each hash indicates a record. + +[source, ruby] +------------------------------------------------------- +Client.connection.select_all("SELECT * FROM `clients` WHERE `id` = '1'") +------------------------------------------------------- == Working with Associations -When you define a has_many association on a model you get the find method and dynamic finders also on that association. This is helpful for finding associated records within the scope of an exisiting record, for example finding all the orders for a client that have been sent and not received by doing something like `Client.find(params[:id]).orders.find_by_sent_and_received(true, false)`. Having this find method available on associations is extremely helpful when using nested controllers. +When you define a has_many association on a model you get the find method and dynamic finders also on that association. This is helpful for finding associated records within the scope of an exisiting record, for example finding all the orders for a client that have been sent and not received by doing something like +Client.find(params[:id]).orders.find_by_sent_and_received(true, false)+. Having this find method available on associations is extremely helpful when using nested controllers. == Named Scopes -In this section we'll cover adding named scopes to the models in the application. Let's say we want to find all clients who are male we would use this code: +Named scopes are another way to add custom finding behavior to the models in the application. Suppose want to find all clients who are male. Yould use this code: [source, ruby] +------------------------------------------------------- class Client < ActiveRecord::Base named_scope :males, :conditions => { :gender => "male" } end +------------------------------------------------------- -And we could call it like `Client.males` to get all the clients who are male. +And you could call it like +Client.males.all+ to get all the clients who are male. Please note that if you do not specify the +all+ on the end you will get a +Scope+ object back, not a set of records which you do get back if you put the +all+ on the end. -If we wanted to find all the clients who are active, we could use this: +If you wanted to find all the clients who are active, you could use this: [source,ruby] +------------------------------------------------------- class Client < ActiveRecord::Base named_scope :active, :conditions => { :active => true } end +------------------------------------------------------- -We would call this new named_scope by doing `Client.active` and this will do the same query as if we just used `Client.find(:all, :conditions => ["active = ?", true])`. Please be aware that the conditions syntax in named_scope and find is different and the two are not interchangeable. If you want to find the first client within this named scope you could do `Client.active.first`. +You can call this new named_scope by doing +Client.active.all+ and this will do the same query as if we just used +Client.all(:conditions => ["active = ?", true])+. Please be aware that the conditions syntax in named_scope and find is different and the two are not interchangeable. If you want to find the first client within this named scope you could do +Client.active.first+. -and then if we wanted to find all the clients who are active and male we could stack the named scopes like this: +If you wanted to find all the clients who are active and male you can stack the named scopes like this: [source, ruby] -Client.males.active +------------------------------------------------------- +Client.males.active.all +------------------------------------------------------- -If you would then like to do a `find` on that subset of clients, you can. Just like an association, named scopes allow you to call `find` on a set of records: +If you would then like to do a +all+ on that scope, you can. Just like an association, named scopes allow you to call +all+ on them: [source, ruby] -Client.males.active.find(:all, :conditions => ["age > ?", params[:age]]) +------------------------------------------------------- +Client.males.active.all(:conditions => ["age > ?", params[:age]]) +------------------------------------------------------- -Now observe the following code: +Consider the following code: [source, ruby] +------------------------------------------------------- class Client < ActiveRecord::Base named_scope :recent, :conditions => { :created_at > 2.weeks.ago } end +------------------------------------------------------- -What we see here is what looks to be a standard named scope that defines a method called recent which gathers all records created any time between now and 2 weeks ago. That's correct for the first time the model is loaded but for any time after that, `2.weeks.ago` is set to that same value, so you will consistently get records from a certain date until your model is reloaded by something like your application restarting. The way to fix this is to put the code in a lambda block: +This looks like a standard named scope that defines a method called recent which gathers all records created any time between now and 2 weeks ago. That's correct for the first time the model is loaded but for any time after that, +2.weeks.ago+ is set to that same value, so you will consistently get records from a certain date until your model is reloaded by something like your application restarting. The way to fix this is to put the code in a lambda block: [source, ruby] +------------------------------------------------------- class Client < ActiveRecord::Base named_scope :recent, lambda { { :conditions => ["created_at > ?", 2.weeks.ago] } } end +------------------------------------------------------- -And now every time the recent named scope is called, because it's wrapped in a lambda block this code will be parsed every time so you'll get actually 2 weeks ago from the code execution, not 2 weeks ago from the time the model was loaded. +And now every time the recent named scope is called, the code in the lambda block will be parsed, so you'll get actually 2 weeks ago from the code execution, not 2 weeks ago from the time the model was loaded. -In a named scope you can use `:include` and `:joins` options just like in find. +In a named scope you can use +:include+ and +:joins+ options just like in find. [source, ruby] +------------------------------------------------------- class Client < ActiveRecord::Base - named_scope :active_within_2_weeks, :joins => :order, lambda { { :conditions => ["orders.created_at > ?", 2.weeks.ago] } } + named_scope :active_within_2_weeks, :joins => :order, + lambda { { :conditions => ["orders.created_at > ?", 2.weeks.ago] } } end +------------------------------------------------------- -This method called as `Client.active_within_2_weeks` will return all clients who have placed orders in the past 2 weeks. +This method, called as +Client.active_within_2_weeks.all+, will return all clients who have placed orders in the past 2 weeks. If you want to pass a named scope a compulsory argument, just specify it as a block parameter like this: [source, ruby] +------------------------------------------------------- class Client < ActiveRecord::Base - named_scope :recent, lambda { |time| { :conditions => ["created_at > ?", time] } } } + named_scope :recent, lambda { |time| { :conditions => ["created_at > ?", time] } } end +------------------------------------------------------- -This will work if we call `Client.recent(2.weeks.ago)` but not if we call `Client.recent`. If we want to add an optional argument for this, we have to use the splat operator as the block's parameter. +This will work if you call +Client.recent(2.weeks.ago).all+ but not if you call +Client.recent+. If you want to add an optional argument for this, you have to use the splat operator as the block's parameter. [source, ruby] +------------------------------------------------------- class Client < ActiveRecord::Base - named_scope :recent, lambda { |*args| { :conditions => ["created_at > ?", args.first || 2.weeks.ago] } } } + named_scope :recent, lambda { |*args| { :conditions => ["created_at > ?", args.first || 2.weeks.ago] } } end +------------------------------------------------------- + +This will work with +Client.recent(2.weeks.ago).all+ and +Client.recent.all+, with the latter always returning records with a created_at date between right now and 2 weeks ago. -This will work with `Client.recent(2.weeks.ago)` and `Client.recent` with the latter always returning records with a created_at date between right now and 2 weeks ago. +Remember that named scopes are stackable, so you will be able to do +Client.recent(2.weeks.ago).unlocked.all+ to find all clients created between right now and 2 weeks ago and have their locked field set to false. -Remember that named scopes are stackable, so you will be able to do `Client.recent(2.weeks.ago).unlocked` to find all clients created between right now and 2 weeks ago and have their locked field set to false. +Finally, if you wish to define named scopes on the fly you can use the scoped method: +[source, ruby] +------------------------------------------------------- +class Client < ActiveRecord::Base + def self.recent + scoped :conditions => ["created_at > ?", 2.weeks.ago] + end +end +------------------------------------------------------- -== Existance of Objects +== Existence of Objects -If you simply want to check for the existance of the object there's a method called `exists?`. This method will query the database using the same query as find, but instead of returning an object or collection of objects it will return either true or false. +If you simply want to check for the existence of the object there's a method called +exists?+. This method will query the database using the same query as find, but instead of returning an object or collection of objects it will return either true or false. [source, ruby] +------------------------------------------------------- Client.exists?(1) +------------------------------------------------------- The above code will check for the existance of a clients table record with the id of 1 and return true if it exists. [source, ruby] +------------------------------------------------------- Client.exists?(1,2,3) # or Client.exists?([1,2,3]) +------------------------------------------------------- -`exists?` also takes multiple ids, as shown by the above code, but the catch is that it will return true if any one of those records exists. +The +exists?+ method also takes multiple ids, as shown by the above code, but the catch is that it will return true if any one of those records exists. -Further more, `exists` takes a `conditions` option much like find: +Further more, +exists+ takes a +conditions+ option much like find: [source, ruby] +------------------------------------------------------- Client.exists?(:conditions => "first_name = 'Ryan'") +------------------------------------------------------- == Calculations This section uses count as an example method in this preamble, but the options described apply to all sub-sections. -`count` takes conditions much in the same way `exists?` does: ++count+ takes conditions much in the same way +exists?+ does: [source, ruby] +------------------------------------------------------- Client.count(:conditions => "first_name = 'Ryan'") +------------------------------------------------------- Which will execute: [source, sql] -SELECT count(*) AS count_all FROM `clients` WHERE (first_name = 1) +------------------------------------------------------- +SELECT count(*) AS count_all FROM +clients+ WHERE (first_name = 1) +------------------------------------------------------- -You can also use `include` or `joins` for this to do something a little more complex: +You can also use +include+ or +joins+ for this to do something a little more complex: [source, ruby] +------------------------------------------------------- Client.count(:conditions => "clients.first_name = 'Ryan' AND orders.status = 'received'", :include => "orders") +------------------------------------------------------- Which will execute: [source, sql] -SELECT count(DISTINCT `clients`.id) AS count_all FROM `clients` LEFT OUTER JOIN `orders` ON orders.client_id = client.id WHERE (clients.first_name = 'name' AND orders.status = 'received') +------------------------------------------------------- +SELECT count(DISTINCT +clients+.id) AS count_all FROM +clients+ + LEFT OUTER JOIN +orders+ ON orders.client_id = client.id WHERE + (clients.first_name = 'name' AND orders.status = 'received') +------------------------------------------------------- -We specify `clients.first_name` just in case one of our join tables has a field also called `first_name` and we do `orders.status` because that's the name of our join table. +This code specifies +clients.first_name+ just in case one of the join tables has a field also called +first_name+ and it uses +orders.status+ because that's the name of our join table. === Count -If you want to see how many records are in your model's table you could call `Client.count` and that will return the number. If you want to be more specific and find all the clients with their age present in the database you can use `Client.count(:age)`. +If you want to see how many records are in your model's table you could call +Client.count+ and that will return the number. If you want to be more specific and find all the clients with their age present in the database you can use +Client.count(:age)+. For options, please see the parent section, Calculations. === Average -If you want to see the average of a certain number in one of your tables you can call the `average` method on the class that relates to the table. This method call will look something like this: +If you want to see the average of a certain number in one of your tables you can call the +average+ method on the class that relates to the table. This method call will look something like this: [source, ruby] +------------------------------------------------------- Client.average("orders_count") +------------------------------------------------------- -This will return a number (possibly a floating point number such as 3.14159265) representing the average of the fields. +This will return a number (possibly a floating point number such as 3.14159265) representing the average value in the field. For options, please see the parent section, <<_calculations, Calculations>> === Minimum -If you want to find the minimum value of a field in your table you can call the `minimum` method on the class that relates to the table. This method call will look something like this: +If you want to find the minimum value of a field in your table you can call the +minimum+ method on the class that relates to the table. This method call will look something like this: [source, ruby] +------------------------------------------------------- Client.minimum("age") +------------------------------------------------------- For options, please see the parent section, <<_calculations, Calculations>> === Maximum -If you want to find the maximum value of a field in your table you can call the `maximum` method on the class that relates to the table. This method call will look something like this: +If you want to find the maximum value of a field in your table you can call the +maximum+ method on the class that relates to the table. This method call will look something like this: [source, ruby] +------------------------------------------------------- Client.maximum("age") +------------------------------------------------------- For options, please see the parent section, <<_calculations, Calculations>> - === Sum -If you want to find the sum of a field for all records in your table you can call the `sum` method on the class that relates to the table. This method call will look something like this: +If you want to find the sum of a field for all records in your table you can call the +sum+ method on the class that relates to the table. This method call will look something like this: [source, ruby] +------------------------------------------------------- Client.sum("orders_count") +------------------------------------------------------- For options, please see the parent section, <<_calculations, Calculations>> @@ -454,35 +631,17 @@ Thanks to Ryan Bates for his awesome screencast on named scope #108. The informa Thanks to Mike Gunderloy for his tips on creating this guide. -== Change Log - -=== Sunday, 28 September 2008 -1. Changed "In Rails you don't have to type SQL" to "In Rails you don't usually have to type SQL" -2. Inserted paragraph in dynamic finders about find_or_create and find_or_initialize -3. Extended "First, Last, All" section. -4. Renamed "First, Last & All" to "IDs, First, Last and All" -5. Added finding by id and passing in ids to "IDs, First, Last and All" - - -=== Wednesday, 01 October 2008 -1. Did section on limit and offset, as well as section on readonly. -2. Altered formatting so it doesn't look bad. - - -=== Sunday, 05 October 2008 -1. Extended conditions section to include IN and using operators inside the conditions. -2. Extended conditions section to include paragraph and example of parameter safety. -3. Added TODO sections. - -=== Monday, 06 October 2008 -1. Added section in Eager Loading about using conditions on tables that are not the model's own. - -=== Thursday, 09 October 2008 -1. Wrote section about lock option and tidied up "Making it all work together" section. -2. Added section on using count. +== Changelog -=== Tuesday, 21 October 2008 -1. Extended named scope guide by adding :include and :joins and find sub-sections. +http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16[Lighthouse ticket] -=== Wednesday, 22 October 2008 -1. Completed calculations section. +* October 27, 2008: Added scoped section, added named params for conditions and added sub-section headers for conditions section. +* October 27, 2008: Fixed up all points specified in http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16-activerecord-finders#ticket-16-6[this comment] with an exception of the final point. +* October 26, 2008: Editing pass by link:../authors.html#mgunderloy[Mike Gunderloy] . First release version. +* October 22, 2008: Calculations complete, first complete draft by Ryan Bigg +* October 21, 2008: Extended named scope section by Ryan Bigg +* October 9, 2008: Lock, count, cleanup by Ryan Bigg +* October 6, 2008: Eager loading by Ryan Bigg +* October 5, 2008: Covered conditions by Ryan Bigg +* October 1, 2008: Covered limit/offset, formatting changes by Ryan Bigg +* September 28, 2008: Covered first/last/all by Ryan Bigg diff --git a/railties/doc/guides/source/getting_started_with_rails.txt b/railties/doc/guides/source/getting_started_with_rails.txt index 45e6485886..c5bbc9e814 100644 --- a/railties/doc/guides/source/getting_started_with_rails.txt +++ b/railties/doc/guides/source/getting_started_with_rails.txt @@ -1225,6 +1225,7 @@ Now that you've seen your first Rails application, you should feel free to updat http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/2[Lighthouse ticket] +* November 1, 2008: First approved version by link:../authors.html#mgunderloy[Mike Gunderloy] * October 16, 2008: Revised based on feedback from Pratik Naik by link:../authors.html#mgunderloy[Mike Gunderloy] (not yet approved for publication) * October 13, 2008: First complete draft by link:../authors.html#mgunderloy[Mike Gunderloy] (not yet approved for publication) * October 12, 2008: More detail, rearrangement, editing by link:../authors.html#mgunderloy[Mike Gunderloy] (not yet approved for publication) diff --git a/railties/doc/guides/source/index.txt b/railties/doc/guides/source/index.txt index e8c4040b7b..05d7deee6a 100644 --- a/railties/doc/guides/source/index.txt +++ b/railties/doc/guides/source/index.txt @@ -11,8 +11,6 @@ CAUTION: Guides marked with this icon are currently being worked on. While they .link:getting_started_with_rails.html[Getting Started with Rails] *********************************************************** -CAUTION: link:http://rails.lighthouseapp.com/projects/16213/tickets/2[Lighthouse Ticket] - Everything you need to know to install Rails and create your first application. *********************************************************** @@ -94,8 +92,6 @@ Enjoy. .link:security.html[Securing Rails Applications] *********************************************************** -CAUTION: link:http://rails.lighthouseapp.com/projects/16213/tickets/7[Lighthouse Ticket] - This manual describes common security problems in web applications and how to avoid them with Rails. *********************************************************** diff --git a/railties/doc/guides/source/layouts_and_rendering.txt b/railties/doc/guides/source/layouts_and_rendering.txt index 3d970b60ce..2f39c70e8c 100644 --- a/railties/doc/guides/source/layouts_and_rendering.txt +++ b/railties/doc/guides/source/layouts_and_rendering.txt @@ -166,6 +166,17 @@ render :xml => @product TIP: You don't need to call +to_xml+ on the object that you want to render. If you use the +:xml+ option, +render+ will automatically call +to_xml+ for you. +==== Rendering Vanilla JavaScript + +Rails can render vanilla JavaScript (as an alternative to using +update+ with n +.rjs+ file): + +[source, ruby] +------------------------------------------------------- +render :js => "alert('Hello Rails');" +------------------------------------------------------- + +This will send the supplied string to the browser with a MIME type of +text/javascript+. + ==== Options for +render+ Calls to the +render+ method generally accept four options: @@ -302,22 +313,39 @@ With those declarations, the +inventory+ layout would be used only for the +inde Layouts are shared downwards in the hierarchy, and more specific layouts always override more general ones. For example: ++application.rb+: + [source, ruby] ------------------------------------------------------- class ApplicationController < ActionController::Base layout "main" #... end +------------------------------------------------------- ++posts_controller.rb+: + +[source, ruby] +------------------------------------------------------- class PostsController < ApplicationController # ... end +------------------------------------------------------- + ++special_posts_controller.rb+: +[source, ruby] +------------------------------------------------------- class SpecialPostsController < PostsController layout "special" # ... end +------------------------------------------------------- + ++old_posts_controller.rb+: +[source, ruby] +------------------------------------------------------- class OldPostsController < SpecialPostsController layout nil @@ -534,7 +562,7 @@ You can supply the +:recursive+ option to load files in subfolders of +public/ja [source, ruby] ------------------------------------------------------- -<%= javascript_include_tag :all, :recursive %> +<%= javascript_include_tag :all, :recursive => true %> ------------------------------------------------------- If you're loading multiple javascript files, you can create a better user experience by combining multiple files into a single download. To make this happen in production, specify +:cache => true+ in your +javascript_include_tag+: @@ -601,7 +629,7 @@ You can supply the +:recursive+ option to link files in subfolders of +public/st [source, ruby] ------------------------------------------------------- -<%= stylesheet_link_tag :all, :recursive %> +<%= stylesheet_link_tag :all, :recursive => true %> ------------------------------------------------------- If you're loading multiple CSS files, you can create a better user experience by combining multiple files into a single download. To make this happen in production, specify +:cache => true+ in your +stylesheet_link_tag+: @@ -766,23 +794,29 @@ This would look for a partial named +_link_area.html.erb+ and render it using th You can also pass local variables into partials, making them even more powerful and flexible. For example, you can use this technique to reduce duplication between new and edit pages, while still keeping a bit of distinct content: ++new.html.erb+: + [source, html] ------------------------------------------------------- -new.html.erb: - <h1>New zone</h1> <%= error_messages_for :zone %> <%= render :partial => "form", :locals => { :button_label => "Create zone", :zone => @zone } %> +------------------------------------------------------- -edit.html.erb: ++edit.html.erb+: +[source, html] +------------------------------------------------------- <h1>Editing zone</h1> <%= error_messages_for :zone %> <%= render :partial => "form", :locals => { :button_label => "Update zone", :zone => @zone } %> +------------------------------------------------------- -_form.html.erb: ++_form.html.erb:+ -<% form_for(@zone) do |f| %> +[source, html] +------------------------------------------------------- +<% form_for(zone) do |f| %> <p> <b>Zone name</b><br /> <%= f.text_field :name %> @@ -795,7 +829,7 @@ _form.html.erb: Although the same partial will be rendered into both views, the label on the submit button is controlled by a local variable passed into the partial. -Every partial also has a local variable with the same name as the partial (minus the underscore). By default, it will look for an instance variable with the same name as the partial in the parent. You can pass an object in to this local variable via the +:object+ option: +Every partial also has a local variable with the same name as the partial (minus the underscore). You can pass an object in to this local variable via the +:object+ option: [source, html] ------------------------------------------------------- @@ -804,6 +838,8 @@ Every partial also has a local variable with the same name as the partial (minus Within the +customer+ partial, the +@customer+ variable will refer to +@new_customer+ from the parent view. +WARNING: In previous versions of Rails, the default local variable would look for an instance variable with the same name as the partial in the parent. This behavior is deprecated in Rails 2.2 and will be removed in a future version. + If you have an instance of a model to render into a partial, you can use a shorthand syntax: [source, html] @@ -817,15 +853,18 @@ Assuming that the +@customer+ instance variable contains an instance of the +Cus Partials are very useful in rendering collections. When you pass a collection to a partial via the +:collection+ option, the partial will be inserted once for each member in the collection: ++index.html.erb+: + [source, html] ------------------------------------------------------- -index.html.erb: - <h1>Products</h1> <%= render :partial => "product", :collection => @products %> +------------------------------------------------------- -_product.html.erb: ++_product.html.erb+: +[source, html] +------------------------------------------------------- <p>Product Name: <%= product.name %></p> ------------------------------------------------------- @@ -849,33 +888,42 @@ Rails will render the +_product_ruler+ partial (with no data passed in to it) be There's also a shorthand syntax available for rendering collections. For example, if +@products+ is a collection of products, you can render the collection this way: ++index.html.erb+: + [source, html] ------------------------------------------------------- -index.html.erb: - <h1>Products</h1> <%= render :partial => @products %> +------------------------------------------------------- -_product.html.erb: ++_product.html.erb+: +[source, html] +------------------------------------------------------- <p>Product Name: <%= product.name %></p> ------------------------------------------------------- Rails determines the name of the partial to use by looking at the model name in the collection. In fact, you can even create a heterogeneous collection and render it this way, and Rails will choose the proper partial for each member of the collection: ++index.html.erb+: + [source, html] ------------------------------------------------------- -index.html.erb: - <h1>Contacts</h1> <%= render :partial => [customer1, employee1, customer2, employee2] %> +------------------------------------------------------- -_customer.html.erb: ++_customer.html.erb+: +[source, html] +------------------------------------------------------- <p>Name: <%= customer.name %></p> +------------------------------------------------------- -_employee.html.erb: ++_employee.html.erb+: +[source, html] +------------------------------------------------------- <p>Name: <%= employee.name %></p> ------------------------------------------------------- @@ -885,6 +933,7 @@ In this case, Rails will use the customer or employee partials as appropriate fo http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/15[Lighthouse ticket] +* November 1, 2008: Added +:js+ option for +render+ by link:../authors.html#mgunderloy[Mike Gunderloy] * October 16, 2008: Ready for publication by link:../authors.html#mgunderloy[Mike Gunderloy] * October 4, 2008: Additional info on partials (+:object+, +:as+, and +:spacer_template+) by link:../authors.html#mgunderloy[Mike Gunderloy] (not yet approved for publication) * September 28, 2008: First draft by link:../authors.html#mgunderloy[Mike Gunderloy] (not yet approved for publication) diff --git a/railties/doc/guides/source/routing_outside_in.txt b/railties/doc/guides/source/routing_outside_in.txt index 716c362c76..6d127973b0 100644 --- a/railties/doc/guides/source/routing_outside_in.txt +++ b/railties/doc/guides/source/routing_outside_in.txt @@ -600,7 +600,7 @@ map.namespace(:admin) do |admin| end ------------------------------------------------------- -As you can see, the namespaced version is much more succinct than the one that spells everything out - but it still creates the same routes. For example, you'll get +admin_photos_url+ that expects to find an +Admin::PhotosController+ and that matches +admin/photos+, and +admin_photos_ratings+path+ that matches +/admin/photos/_photo_id_/ratings+, expecting to use +Admin::RatingsController+. Even though you're not specifying +path_prefix+ explicitly, the routing code will calculate the appropriate +path_prefix+ from the route nesting. +As you can see, the namespaced version is much more succinct than the one that spells everything out - but it still creates the same routes. For example, you'll get +admin_photos_url+ that expects to find an +Admin::PhotosController+ and that matches +admin/photos+, and +admin_photos_ratings_path+ that matches +/admin/photos/_photo_id_/ratings+, expecting to use +Admin::RatingsController+. Even though you're not specifying +path_prefix+ explicitly, the routing code will calculate the appropriate +path_prefix+ from the route nesting. === Adding More RESTful Actions diff --git a/railties/doc/guides/source/security.txt b/railties/doc/guides/source/security.txt index 53819babb7..9b3f47932e 100644 --- a/railties/doc/guides/source/security.txt +++ b/railties/doc/guides/source/security.txt @@ -976,3 +976,9 @@ The security landscape shifts and it is important to keep up to date, because mi - http://secunia.com/[Keep up to date on the other application layers] (they have a weekly newsletter, too) - A http://ha.ckers.org/blog/[good security blog] including the http://ha.ckers.org/xss.html[Cross-Site scripting Cheat Sheet] - Another http://www.0x000000.com/[good security blog] with some Cheat Sheets, too + +== Changelog == + +http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/7[Lighthouse ticket] + +* November 1, 2008: First approved version by Heiko Webers |