1 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2 <html xmlns=
"http://www.w3.org/1999/xhtml" lang=
"en" xml:
lang=
"en">
4 <meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8" />
5 <title>A Guide to Active Record Associations
</title>
7 <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
9 <link href=
"stylesheets/base.css" media=
"screen" rel=
"Stylesheet" type=
"text/css" />
10 <link href=
"stylesheets/forms.css" media=
"screen" rel=
"Stylesheet" type=
"text/css" />
11 <link href=
"stylesheets/more.css" media=
"screen" rel=
"Stylesheet" type=
"text/css" />
12 <style type=
"text/css">
36 border: solid
1px #cccccc;
52 #content dt tt, #content pre tt {
81 div#header h1#site_title {
82 background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
108 .left-floaty, .right-floaty {
120 .admonitionblock .icon {
124 .admonitionblock .content {
125 border: solid
1px #ffda78;
132 .admonitionblock .title {
134 margin-bottom:
0.5em;
138 border: solid
1px #aaaaff;
156 border: solid
1px #ccccbb;
161 .sidebarblock .sidebar-title {
164 margin-bottom:
0.3em;
167 .sidebarblock .sidebar-content
> .para:last-child
> p {
171 .sidebarblock .sidebar-title a {
172 text-decoration: none;
175 .sidebarblock .sidebar-title a:hover {
176 text-decoration: underline;
184 <a href=
"index.html" title=
"Ruby on Rails"><img src=
"images/rails_logo_remix.gif" alt=
"Rails" height=
"140" width=
"110" /></a>
187 <h1 id=
"site_title"><span>Ruby on Rails
</span></h1>
188 <h2 id=
"site_title_tagline">Sustainable productivity for web-application development
</h2>
191 <li class=
"first-child"><a href=
"http://www.rubyonrails.org/" title=
"Ruby on Rails" class=
"ruby_on_rails">Ruby on Rails
</a></li>
192 <li><a class=
"manuals" href=
"index.html" title=
"Manuals Index">Guides Index
</a></li>
202 <a href=
"#_why_associations">Why Associations?
</a>
205 <a href=
"#_the_types_of_associations">The Types of Associations
</a>
208 <li><a href=
"#_the_tt_belongs_to_tt_association">The
<tt>belongs_to
</tt> Association
</a></li>
210 <li><a href=
"#_the_tt_has_one_tt_association">The
<tt>has_one
</tt> Association
</a></li>
212 <li><a href=
"#_the_tt_has_many_tt_association">The
<tt>has_many
</tt> Association
</a></li>
214 <li><a href=
"#_the_tt_has_many_through_tt_association">The
<tt>has_many :through
</tt> Association
</a></li>
216 <li><a href=
"#_the_tt_has_one_through_tt_association">The
<tt>has_one :through
</tt> Association
</a></li>
218 <li><a href=
"#_the_tt_has_and_belongs_to_many_tt_association">The
<tt>has_and_belongs_to_many
</tt> Association
</a></li>
220 <li><a href=
"#_choosing_between_tt_belongs_to_tt_and_tt_has_one_tt">Choosing Between
<tt>belongs_to
</tt> and
<tt>has_one
</tt></a></li>
222 <li><a href=
"#_choosing_between_tt_has_many_through_tt_and_tt_has_and_belongs_to_many_tt">Choosing Between
<tt>has_many :through
</tt> and
<tt>has_and_belongs_to_many
</tt></a></li>
224 <li><a href=
"#_polymorphic_associations">Polymorphic Associations
</a></li>
226 <li><a href=
"#_self_joins">Self Joins
</a></li>
231 <a href=
"#_tips_tricks_and_warnings">Tips, Tricks, and Warnings
</a>
234 <li><a href=
"#_controlling_caching">Controlling Caching
</a></li>
236 <li><a href=
"#_avoiding_name_collisions">Avoiding Name Collisions
</a></li>
238 <li><a href=
"#_updating_the_schema">Updating the Schema
</a></li>
240 <li><a href=
"#_controlling_association_scope">Controlling Association Scope
</a></li>
245 <a href=
"#_detailed_association_reference">Detailed Association Reference
</a>
248 <li><a href=
"#_the_tt_belongs_to_tt_association_2">The
<tt>belongs_to
</tt> Association
</a></li>
250 <li><a href=
"#_the_has_one_association">The has_one Association
</a></li>
252 <li><a href=
"#_the_has_many_association">The has_many Association
</a></li>
254 <li><a href=
"#_the_tt_has_and_belongs_to_many_tt_association_2">The
<tt>has_and_belongs_to_many
</tt> Association
</a></li>
256 <li><a href=
"#_association_callbacks">Association Callbacks
</a></li>
258 <li><a href=
"#_association_extensions">Association Extensions
</a></li>
263 <a href=
"#_changelog">Changelog
</a>
269 <h1>A Guide to Active Record Associations
</h1>
271 <div class=
"sectionbody">
272 <div class=
"para"><p>This guide covers the association features of Active Record. By referring to this guide, you will be able to:
</p></div>
273 <div class=
"ilist"><ul>
276 Declare associations between Active Record models
281 Understand the various types of Active Record associations
286 Use the methods added to your models by creating associations
292 <h2 id=
"_why_associations">1. Why Associations?
</h2>
293 <div class=
"sectionbody">
294 <div class=
"para"><p>Why do we need associations between models? Because they make common operations simpler and easier in your code. For example, consider a simple Rails application that includes a model for customers and a model for orders. Each customer can have many orders. Without associations, the model declarations would look like this:
</p></div>
295 <div class=
"listingblock">
296 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
298 http://www.lorenzobettini.it
299 http://www.gnu.org/software/src-highlite -->
300 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Customer
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
301 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
303 <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
304 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
305 </tt></pre></div></div>
306 <div class=
"para"><p>Now, suppose we wanted to add a new order for an existing customer. We'd need to do something like this:
</p></div>
307 <div class=
"listingblock">
308 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
310 http://www.lorenzobettini.it
311 http://www.gnu.org/software/src-highlite -->
312 <pre><tt><span style=
"color: #009900">@order
</span> <span style=
"color: #990000">=
</span> Order
<span style=
"color: #990000">.
</span>create
<span style=
"color: #990000">(:
</span>order_date
<span style=
"color: #990000">=
></span> Time
<span style=
"color: #990000">.
</span>now
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>customer_id
<span style=
"color: #990000">=
></span> <span style=
"color: #009900">@customer
</span><span style=
"color: #990000">.
</span>id
<span style=
"color: #990000">)
</span>
313 </tt></pre></div></div>
314 <div class=
"para"><p>Or consider deleting a customer, and ensuring that all of its orders get deleted as well:
</p></div>
315 <div class=
"listingblock">
316 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
318 http://www.lorenzobettini.it
319 http://www.gnu.org/software/src-highlite -->
320 <pre><tt><span style=
"color: #009900">@orders
</span> <span style=
"color: #990000">=
</span> Order
<span style=
"color: #990000">.
</span>find_by_customer_id
<span style=
"color: #990000">(
</span><span style=
"color: #009900">@customer
</span><span style=
"color: #990000">.
</span>id
<span style=
"color: #990000">)
</span>
321 <span style=
"color: #009900">@orders
</span><span style=
"color: #990000">.
</span>each
<span style=
"font-weight: bold"><span style=
"color: #0000FF">do
</span></span> <span style=
"color: #990000">|
</span>order
<span style=
"color: #990000">|
</span>
322 order
<span style=
"color: #990000">.
</span>destroy
323 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
324 <span style=
"color: #009900">@customer
</span><span style=
"color: #990000">.
</span>destroy
325 </tt></pre></div></div>
326 <div class=
"para"><p>With Active Record associations, we can streamline these - and other - operations by declaratively telling Rails that there is a connection between the two models. Here's the revised code for setting up customers and orders:
</p></div>
327 <div class=
"listingblock">
328 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
330 http://www.lorenzobettini.it
331 http://www.gnu.org/software/src-highlite -->
332 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Customer
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
333 has_many
<span style=
"color: #990000">:
</span>orders
334 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
336 <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
337 belongs_to
<span style=
"color: #990000">:
</span>customer
338 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
339 </tt></pre></div></div>
340 <div class=
"para"><p>With this change, creating a new order for a particular customer is easier:
</p></div>
341 <div class=
"listingblock">
342 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
344 http://www.lorenzobettini.it
345 http://www.gnu.org/software/src-highlite -->
346 <pre><tt><span style=
"color: #009900">@order
</span> <span style=
"color: #990000">=
</span> <span style=
"color: #009900">@customer
</span><span style=
"color: #990000">.
</span>orders
<span style=
"color: #990000">.
</span>create
<span style=
"color: #990000">(:
</span>order_date
<span style=
"color: #990000">=
></span> Time
<span style=
"color: #990000">.
</span>now
<span style=
"color: #990000">)
</span>
347 </tt></pre></div></div>
348 <div class=
"para"><p>Deleting a customer and all of its orders is
<em>much
</em> easier:
</p></div>
349 <div class=
"listingblock">
350 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
352 http://www.lorenzobettini.it
353 http://www.gnu.org/software/src-highlite -->
354 <pre><tt><span style=
"color: #009900">@customer
</span><span style=
"color: #990000">.
</span>destroy
355 </tt></pre></div></div>
356 <div class=
"para"><p>To learn more about the different types of associations, read the next section of this Guide. That's followed by some tips and tricks for working with associations, and then by a complete reference to the methods and options for associations in Rails.
</p></div>
358 <h2 id=
"_the_types_of_associations">2. The Types of Associations
</h2>
359 <div class=
"sectionbody">
360 <div class=
"para"><p>In Rails, an
<em>association
</em> is a connection between two Active Record models. Associations are implemented using macro-style calls, so that you can declaratively add features to your models. For example, by declaring that one model
<tt>belongs_to
</tt> another, you instruct Rails to maintain Primary Key-Foreign Key information between instances of the two models, and you also get a number of utility methods added to your model. Rails supports six types of association:
</p></div>
361 <div class=
"ilist"><ul>
379 <tt>has_many :through
</tt>
384 <tt>has_one :through
</tt>
389 <tt>has_and_belongs_to_many
</tt>
393 <div class=
"para"><p>In the remainder of this guide, you'll learn how to declare and use the various forms of associations. But first, a quick introduction to the situations where each association type is appropriate.
</p></div>
394 <h3 id=
"_the_tt_belongs_to_tt_association">2.1. The
<tt>belongs_to
</tt> Association
</h3>
395 <div class=
"para"><p>A
<tt>belongs_to
</tt> association sets up a one-to-one connection with another model, such that each instance of the declaring model
"belongs to" one instance of the other model. For example, if your application includes customers and orders, and each order can be assigned to exactly one customer, you'd declare the order model this way:
</p></div>
396 <div class=
"listingblock">
397 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
399 http://www.lorenzobettini.it
400 http://www.gnu.org/software/src-highlite -->
401 <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
402 belongs_to
<span style=
"color: #990000">:
</span>customer
403 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
404 </tt></pre></div></div>
405 <div class=
"para"><p><span class=
"image">
406 <img src=
"images/belongs_to.png" alt=
"belongs_to Association Diagram" title=
"belongs_to Association Diagram" />
408 <h3 id=
"_the_tt_has_one_tt_association">2.2. The
<tt>has_one
</tt> Association
</h3>
409 <div class=
"para"><p>A
<tt>has_one
</tt> association also sets up a one-to-one connection with another model, but with somewhat different semantics (and consequences). This association indicates that each instance of a model contains or possesses one instance of another model. For example, if each supplier in your application has only one account, you'd declare the supplier model like this:
</p></div>
410 <div class=
"listingblock">
411 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
413 http://www.lorenzobettini.it
414 http://www.gnu.org/software/src-highlite -->
415 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Supplier
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
416 has_one
<span style=
"color: #990000">:
</span>account
417 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
418 </tt></pre></div></div>
419 <div class=
"para"><p><span class=
"image">
420 <img src=
"images/has_one.png" alt=
"has_one Association Diagram" title=
"has_one Association Diagram" />
422 <h3 id=
"_the_tt_has_many_tt_association">2.3. The
<tt>has_many
</tt> Association
</h3>
423 <div class=
"para"><p>A
<tt>has_many
</tt> association indicates a one-to-many connection with another model. You'll often find this association on the
"other side" of a
<tt>belongs_to
</tt> association. This association indicates that each instance of the model has zero or more instances of another model. For example, in an application containing customers and orders, the customer model could be declared like this:
</p></div>
424 <div class=
"listingblock">
425 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
427 http://www.lorenzobettini.it
428 http://www.gnu.org/software/src-highlite -->
429 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Customer
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
430 has_many
<span style=
"color: #990000">:
</span>orders
431 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
432 </tt></pre></div></div>
433 <div class=
"admonitionblock">
436 <img src=
"./images/icons/note.png" alt=
"Note" />
438 <td class=
"content">The name of the other model is pluralized when declaring a
<tt>has_many
</tt> association.
</td>
441 <div class=
"para"><p><span class=
"image">
442 <img src=
"images/has_many.png" alt=
"has_many Association Diagram" title=
"has_many Association Diagram" />
444 <h3 id=
"_the_tt_has_many_through_tt_association">2.4. The
<tt>has_many :through
</tt> Association
</h3>
445 <div class=
"para"><p>A
<tt>has_many :through
</tt> association is often used to set up a many-to-many connection with another model. This association indicates that the declaring model can be matched with zero or more instances of another model by proceeding
<em>through
</em> a third model. For example, consider a medical practice where patients make appointments to see physicians. The relevant association declarations could look like this:
</p></div>
446 <div class=
"listingblock">
447 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
449 http://www.lorenzobettini.it
450 http://www.gnu.org/software/src-highlite -->
451 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Physician
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
452 has_many
<span style=
"color: #990000">:
</span>appointments
453 has_many
<span style=
"color: #990000">:
</span>patients
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>through
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">:
</span>appointments
454 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
456 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Appointment
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
457 belongs_to
<span style=
"color: #990000">:
</span>physician
458 belongs_to
<span style=
"color: #990000">:
</span>patient
459 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
461 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Patient
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
462 has_many
<span style=
"color: #990000">:
</span>appointments
463 has_many
<span style=
"color: #990000">:
</span>physicians
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>through
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">:
</span>appointments
464 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
465 </tt></pre></div></div>
466 <div class=
"para"><p><span class=
"image">
467 <img src=
"images/has_many_through.png" alt=
"has_many :through Association Diagram" title=
"has_many :through Association Diagram" />
469 <div class=
"para"><p>The
<tt>has_many :through
</tt> association is also useful for setting up
"shortcuts" through nested :
<tt>has_many
</tt> associations. For example, if a document has many sections, and a section has many paragraphs, you may sometimes want to get a simple collection of all paragraphs in the document. You could set that up this way:
</p></div>
470 <div class=
"listingblock">
471 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
473 http://www.lorenzobettini.it
474 http://www.gnu.org/software/src-highlite -->
475 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Document
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
476 has_many
<span style=
"color: #990000">:
</span>sections
477 has_many
<span style=
"color: #990000">:
</span>paragraphs
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>through
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">:
</span>sections
478 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
480 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Section
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
481 belongs_to
<span style=
"color: #990000">:
</span>document
482 has_many
<span style=
"color: #990000">:
</span>paragraphs
483 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
485 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Paragraph
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
486 belongs_to
<span style=
"color: #990000">:
</span>section
487 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
488 </tt></pre></div></div>
489 <h3 id=
"_the_tt_has_one_through_tt_association">2.5. The
<tt>has_one :through
</tt> Association
</h3>
490 <div class=
"para"><p>A
<tt>has_one :through
</tt> association sets up a one-to-one connection with another model. This association indicates that the declaring model can be matched with one instance of another model by proceeding
<em>through
</em> a third model. For example, if each supplier has one account, and each account is associated with one account history, then the customer model could look like this:
</p></div>
491 <div class=
"listingblock">
492 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
494 http://www.lorenzobettini.it
495 http://www.gnu.org/software/src-highlite -->
496 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Supplier
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
497 has_one
<span style=
"color: #990000">:
</span>account
498 has_one
<span style=
"color: #990000">:
</span>account_history
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>through
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">:
</span>account
499 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
501 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Account
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
502 belongs_to
<span style=
"color: #990000">:
</span>supplier
503 has_one
<span style=
"color: #990000">:
</span>account_history
504 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
506 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> AccountHistory
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
507 belongs_to
<span style=
"color: #990000">:
</span>account
508 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
509 </tt></pre></div></div>
510 <div class=
"para"><p><span class=
"image">
511 <img src=
"images/has_one_through.png" alt=
"has_one :through Association Diagram" title=
"has_one :through Association Diagram" />
513 <h3 id=
"_the_tt_has_and_belongs_to_many_tt_association">2.6. The
<tt>has_and_belongs_to_many
</tt> Association
</h3>
514 <div class=
"para"><p>A
<tt>has_and_belongs_to_many
</tt> association creates a direct many-to-many connection with another model, with no intervening model. For example, if your application includes assemblies and parts, with each assembly having many parts and each part appearing in many assemblies, you could declare the models this way:
</p></div>
515 <div class=
"listingblock">
516 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
518 http://www.lorenzobettini.it
519 http://www.gnu.org/software/src-highlite -->
520 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Assembly
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
521 has_and_belongs_to_many
<span style=
"color: #990000">:
</span>parts
522 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
524 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Part
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
525 has_and_belongs_to_many
<span style=
"color: #990000">:
</span>assemblies
526 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
527 </tt></pre></div></div>
528 <div class=
"para"><p><span class=
"image">
529 <img src=
"images/habtm.png" alt=
"has_and_belongs_to_many Association Diagram" title=
"has_and_belongs_to_many Association Diagram" />
531 <h3 id=
"_choosing_between_tt_belongs_to_tt_and_tt_has_one_tt">2.7. Choosing Between
<tt>belongs_to
</tt> and
<tt>has_one
</tt></h3>
532 <div class=
"para"><p>If you want to set up a
1-
1 relationship between two models, you'll need to add
<tt>belongs_to
</tt> to one, and
<tt>has_one
</tt> to the other. How do you know which is which?
</p></div>
533 <div class=
"para"><p>The distinction is in where you place the foreign key (it goes on the table for the class declaring the
<tt>belongs_to
</tt> association), but you should give some thought to the actual meaning of the data as well. The
<tt>has_one
</tt> relationship says that one of something is yours - that is, that something points back to you. For example, it makes more sense to say that a supplier owns an account than that an account owns a supplier. This suggests that the correct relationships are like this:
</p></div>
534 <div class=
"listingblock">
535 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
537 http://www.lorenzobettini.it
538 http://www.gnu.org/software/src-highlite -->
539 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Supplier
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
540 has_one
<span style=
"color: #990000">:
</span>account
541 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
543 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Account
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
544 belongs_to
<span style=
"color: #990000">:
</span>supplier
545 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
546 </tt></pre></div></div>
547 <div class=
"para"><p>The corresponding migration might look like this:
</p></div>
548 <div class=
"listingblock">
549 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
551 http://www.lorenzobettini.it
552 http://www.gnu.org/software/src-highlite -->
553 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> CreateSuppliers
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Migration
554 <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>up
555 create_table
<span style=
"color: #990000">:
</span>suppliers
<span style=
"font-weight: bold"><span style=
"color: #0000FF">do
</span></span> <span style=
"color: #990000">|
</span>t
<span style=
"color: #990000">|
</span>
556 t
<span style=
"color: #990000">.
</span>string
<span style=
"color: #990000">:
</span>name
557 t
<span style=
"color: #990000">.
</span>timestamps
558 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
560 create_table
<span style=
"color: #990000">:
</span>accounts
<span style=
"font-weight: bold"><span style=
"color: #0000FF">do
</span></span> <span style=
"color: #990000">|
</span>t
<span style=
"color: #990000">|
</span>
561 t
<span style=
"color: #990000">.
</span>integer
<span style=
"color: #990000">:
</span>supplier_id
562 t
<span style=
"color: #990000">.
</span>string
<span style=
"color: #990000">:
</span>account_number
563 t
<span style=
"color: #990000">.
</span>timestamps
564 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
565 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
567 <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>down
568 drop_table
<span style=
"color: #990000">:
</span>accounts
569 drop_table
<span style=
"color: #990000">:
</span>suppliers
570 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
571 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
572 </tt></pre></div></div>
573 <div class=
"admonitionblock">
576 <img src=
"./images/icons/note.png" alt=
"Note" />
578 <td class=
"content">Using
<tt>t.integer :supplier_id
</tt> makes the foreign key naming obvious and implicit. In current versions of Rails, you can abstract away this implementation detail by using
<tt>t.references :supplier
</tt> instead.
</td>
581 <h3 id=
"_choosing_between_tt_has_many_through_tt_and_tt_has_and_belongs_to_many_tt">2.8. Choosing Between
<tt>has_many :through
</tt> and
<tt>has_and_belongs_to_many
</tt></h3>
582 <div class=
"para"><p>Rails offers two different ways to declare a many-to-many relationship between models. The simpler way is to use
<tt>has_and_belongs_to_many
</tt>, which allows you to make the association directly:
</p></div>
583 <div class=
"listingblock">
584 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
586 http://www.lorenzobettini.it
587 http://www.gnu.org/software/src-highlite -->
588 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Assembly
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
589 has_and_belongs_to_many
<span style=
"color: #990000">:
</span>parts
590 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
592 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Part
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
593 has_and_belongs_to_many
<span style=
"color: #990000">:
</span>assemblies
594 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
595 </tt></pre></div></div>
596 <div class=
"para"><p>The second way to declare a many-to-many relationship is to use
<tt>has_many :through
</tt>. This makes the association indirectly, through a join model:
</p></div>
597 <div class=
"listingblock">
598 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
600 http://www.lorenzobettini.it
601 http://www.gnu.org/software/src-highlite -->
602 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Assembly
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
603 has_many
<span style=
"color: #990000">:
</span>manifests
604 has_many
<span style=
"color: #990000">:
</span>parts
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>through
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">:
</span>manifests
605 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
607 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Manifest
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
608 belongs_to
<span style=
"color: #990000">:
</span>assembly
609 belongs_to
<span style=
"color: #990000">:
</span>part
610 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
612 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Part
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
613 has_many
<span style=
"color: #990000">:
</span>manifests
614 has_many
<span style=
"color: #990000">:
</span>assemblies
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>through
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">:
</span>manifests
615 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
616 </tt></pre></div></div>
617 <div class=
"para"><p>The simplest rule of thumb is that you should set up a
<tt>has_many :through
</tt> relationship if you need to work with the relationship model as an independent entity. If you don't need to do anything with the relationship model, it may be simpler to set up a
<tt>has_and_belongs_to_many
</tt> relationship (though you'll need to remember to create the joining table).
</p></div>
618 <div class=
"para"><p>You should use
<tt>has_many :through
</tt> if you need validations, callbacks, or extra attributes on the join model.
</p></div>
619 <h3 id=
"_polymorphic_associations">2.9. Polymorphic Associations
</h3>
620 <div class=
"para"><p>A slightly more advanced twist on associations is the
<em>polymorphic association
</em>. With polymorphic associations, a model can belong to more than one other model, on a single association. For example, you might have a picture model that belongs to either an employee model or a product model. Here's how this could be declared:
</p></div>
621 <div class=
"listingblock">
622 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
624 http://www.lorenzobettini.it
625 http://www.gnu.org/software/src-highlite -->
626 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Picture
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
627 belongs_to
<span style=
"color: #990000">:
</span>imageable
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>polymorphic
<span style=
"color: #990000">=
></span> <span style=
"font-weight: bold"><span style=
"color: #0000FF">true
</span></span>
628 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
630 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Employee
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
631 has_many
<span style=
"color: #990000">:
</span>pictures
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>as
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">:
</span>imageable
632 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
634 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Product
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
635 has_many
<span style=
"color: #990000">:
</span>pictures
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>as
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">:
</span>imageable
636 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
637 </tt></pre></div></div>
638 <div class=
"para"><p>You can think of a polymorphic
<tt>belongs_to
</tt> declaration as setting up an interface that any other model can use. From an instance of the
<tt>Employee
</tt> model, you can retrieve a collection of pictures:
<tt>@employee.pictures
</tt>. Similarly, you can retrieve
<tt>@product.pictures
</tt>. If you have an instance of the
<tt>Picture
</tt> model, you can get to its parent via
<tt>@picture.imageable
</tt>. To make this work, you need to declare both a foreign key column and a type column in the model that declares the polymorphic interface:
</p></div>
639 <div class=
"listingblock">
640 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
642 http://www.lorenzobettini.it
643 http://www.gnu.org/software/src-highlite -->
644 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> CreatePictures
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Migration
645 <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>up
646 create_table
<span style=
"color: #990000">:
</span>pictures
<span style=
"font-weight: bold"><span style=
"color: #0000FF">do
</span></span> <span style=
"color: #990000">|
</span>t
<span style=
"color: #990000">|
</span>
647 t
<span style=
"color: #990000">.
</span>string
<span style=
"color: #990000">:
</span>name
648 t
<span style=
"color: #990000">.
</span>integer
<span style=
"color: #990000">:
</span>imageable_id
649 t
<span style=
"color: #990000">.
</span>string
<span style=
"color: #990000">:
</span>imageable_type
650 t
<span style=
"color: #990000">.
</span>timestamps
651 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
652 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
654 <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>down
655 drop_table
<span style=
"color: #990000">:
</span>pictures
656 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
657 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
658 </tt></pre></div></div>
659 <div class=
"para"><p>This migration can be simplified by using the
<tt>t.references
</tt> form:
</p></div>
660 <div class=
"listingblock">
661 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
663 http://www.lorenzobettini.it
664 http://www.gnu.org/software/src-highlite -->
665 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> CreatePictures
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Migration
666 <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>up
667 create_table
<span style=
"color: #990000">:
</span>pictures
<span style=
"font-weight: bold"><span style=
"color: #0000FF">do
</span></span> <span style=
"color: #990000">|
</span>t
<span style=
"color: #990000">|
</span>
668 t
<span style=
"color: #990000">.
</span>string
<span style=
"color: #990000">:
</span>name
669 t
<span style=
"color: #990000">.
</span>references
<span style=
"color: #990000">:
</span>imageable
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>polymorphic
<span style=
"color: #990000">=
></span> <span style=
"font-weight: bold"><span style=
"color: #0000FF">true
</span></span>
670 t
<span style=
"color: #990000">.
</span>timestamps
671 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
672 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
674 <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>down
675 drop_table
<span style=
"color: #990000">:
</span>pictures
676 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
677 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
678 </tt></pre></div></div>
679 <div class=
"para"><p><span class=
"image">
680 <img src=
"images/polymorphic.png" alt=
"Polymorphic Association Diagram" title=
"Polymorphic Association Diagram" />
682 <h3 id=
"_self_joins">2.10. Self Joins
</h3>
683 <div class=
"para"><p>In designing a data model, you will sometimes find a model that should have a relation to itself. For example, you may want to store all employees in a single database model, but be able to trace relationships such as manager and subordinates. This situation can be modeled with self-joining associations:
</p></div>
684 <div class=
"listingblock">
685 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
687 http://www.lorenzobettini.it
688 http://www.gnu.org/software/src-highlite -->
689 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Employee
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
690 has_many
<span style=
"color: #990000">:
</span>subordinates
<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=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>foreign_key
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"manager_id"</span>
691 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>
692 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
693 </tt></pre></div></div>
694 <div class=
"para"><p>With this setup, you can retrieve
<tt>@employee.subordinates
</tt> and
<tt>@employee.manager
</tt>.
</p></div>
696 <h2 id=
"_tips_tricks_and_warnings">3. Tips, Tricks, and Warnings
</h2>
697 <div class=
"sectionbody">
698 <div class=
"para"><p>Here are a few things you should know to make efficient use of Active Record associations in your Rails applications:
</p></div>
699 <div class=
"ilist"><ul>
707 Avoiding name collisions
717 Controlling association scope
721 <h3 id=
"_controlling_caching">3.1. Controlling Caching
</h3>
722 <div class=
"para"><p>All of the association methods are built around caching that keeps the result of the most recent query available for further operations. The cache is even shared across methods. For example:
</p></div>
723 <div class=
"listingblock">
724 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
726 http://www.lorenzobettini.it
727 http://www.gnu.org/software/src-highlite -->
728 <pre><tt>customer
<span style=
"color: #990000">.
</span>orders
<span style=
"font-style: italic"><span style=
"color: #9A1900"># retrieves orders from the database
</span></span>
729 customer
<span style=
"color: #990000">.
</span>orders
<span style=
"color: #990000">.
</span>size
<span style=
"font-style: italic"><span style=
"color: #9A1900"># uses the cached copy of orders
</span></span>
730 customer
<span style=
"color: #990000">.
</span>orders
<span style=
"color: #990000">.
</span>empty?
<span style=
"font-style: italic"><span style=
"color: #9A1900"># uses the cached copy of orders
</span></span>
731 </tt></pre></div></div>
732 <div class=
"para"><p>But what if you want to reload the cache, because data might have been changed by some other part of the application? Just pass
<tt>true
</tt> to the association call:
</p></div>
733 <div class=
"listingblock">
734 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
736 http://www.lorenzobettini.it
737 http://www.gnu.org/software/src-highlite -->
738 <pre><tt>customer
<span style=
"color: #990000">.
</span>orders
<span style=
"font-style: italic"><span style=
"color: #9A1900"># retrieves orders from the database
</span></span>
739 customer
<span style=
"color: #990000">.
</span>orders
<span style=
"color: #990000">.
</span>size
<span style=
"font-style: italic"><span style=
"color: #9A1900"># uses the cached copy of orders
</span></span>
740 customer
<span style=
"color: #990000">.
</span>orders
<span style=
"color: #990000">(
</span><span style=
"font-weight: bold"><span style=
"color: #0000FF">true
</span></span><span style=
"color: #990000">).
</span>empty?
<span style=
"font-style: italic"><span style=
"color: #9A1900"># discards the cached copy of orders and goes back to the database
</span></span>
741 </tt></pre></div></div>
742 <h3 id=
"_avoiding_name_collisions">3.2. Avoiding Name Collisions
</h3>
743 <div class=
"para"><p>You are not free to use just any name for your associations. Because creating an association adds a method with that name to the model, it is a bad idea to give an association a name that is already used for an instance method of
<tt>ActiveRecord::Base
</tt>. The association method would override the base method and break things. For instance,
<tt>attributes
</tt> or
<tt>connection
</tt> are bad names for associations.
</p></div>
744 <h3 id=
"_updating_the_schema">3.3. Updating the Schema
</h3>
745 <div class=
"para"><p>Associations are extremely useful, but they are not magic. You are responsible for maintaining your database schema to match your associations. In practice, this means two things. First, you need to create foreign keys as appropriate:
</p></div>
746 <div class=
"listingblock">
747 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
749 http://www.lorenzobettini.it
750 http://www.gnu.org/software/src-highlite -->
751 <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
752 belongs_to
<span style=
"color: #990000">:
</span>customer
753 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
754 </tt></pre></div></div>
755 <div class=
"para"><p>This declaration needs to be backed up by the proper foreign key declaration on the orders table:
</p></div>
756 <div class=
"listingblock">
757 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
759 http://www.lorenzobettini.it
760 http://www.gnu.org/software/src-highlite -->
761 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> CreateOrders
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Migration
762 <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>up
763 create_table
<span style=
"color: #990000">:
</span>orders
<span style=
"font-weight: bold"><span style=
"color: #0000FF">do
</span></span> <span style=
"color: #990000">|
</span>t
<span style=
"color: #990000">|
</span>
764 t
<span style=
"color: #990000">.
</span>order_date
<span style=
"color: #990000">:
</span>datetime
765 t
<span style=
"color: #990000">.
</span>order_number
<span style=
"color: #990000">:
</span>string
766 t
<span style=
"color: #990000">.
</span>customer_id
<span style=
"color: #990000">:
</span>integer
767 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
768 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
770 <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>down
771 drop_table
<span style=
"color: #990000">:
</span>orders
772 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
773 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
774 </tt></pre></div></div>
775 <div class=
"para"><p>If you create an association some time after you build the underlying model, you need to remember to create an
<tt>add_column
</tt> migration to provide the necessary foreign key.
</p></div>
776 <div class=
"para"><p>Second, if you create a
<tt>has_and_belongs_to_many
</tt> association, you need to explicitly create the joining table. Unless the name of the join table is explicitly specified by using the
<tt>:join_table
</tt> option, Active Record create the name by using the lexical order of the class names. So a join between customer and order models will give the default join table name of
"customers_orders" because
"c" outranks
"o" in lexical ordering.
</p></div>
777 <div class=
"admonitionblock">
780 <img src=
"./images/icons/warning.png" alt=
"Warning" />
782 <td class=
"content">The precedence between model names is calculated using the
<tt><</tt> operator for
<tt>String
</tt>. This means that if the strings are of different lengths, and the strings are equal when compared up to the shortest length, then the longer string is considered of higher lexical precedence than the shorter one. For example, one would expect the tables
"paper_boxes" and
"papers" to generate a join table name of
"papers_paper_boxes" because of the length of the name
"paper_boxes", but it in fact generates a join table name of
"paper_boxes_papers".
</td>
785 <div class=
"para"><p>Whatever the name, you must manually generate the join table with an appropriate migration. For example, consider these associations:
</p></div>
786 <div class=
"listingblock">
787 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
789 http://www.lorenzobettini.it
790 http://www.gnu.org/software/src-highlite -->
791 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Assembly
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
792 has_and_belongs_to_many
<span style=
"color: #990000">:
</span>parts
793 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
795 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Part
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
796 has_and_belongs_to_many
<span style=
"color: #990000">:
</span>assemblies
797 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
798 </tt></pre></div></div>
799 <div class=
"para"><p>These need to be backed up by a migration to create the
<tt>assemblies_parts
</tt> table. This table should be created without a primary key:
</p></div>
800 <div class=
"listingblock">
801 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
803 http://www.lorenzobettini.it
804 http://www.gnu.org/software/src-highlite -->
805 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> CreateAssemblyPartJoinTable
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Migration
806 <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>up
807 create_table
<span style=
"color: #990000">:
</span>assemblies_parts
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>id
<span style=
"color: #990000">=
></span> <span style=
"font-weight: bold"><span style=
"color: #0000FF">false
</span></span> <span style=
"font-weight: bold"><span style=
"color: #0000FF">do
</span></span> <span style=
"color: #990000">|
</span>t
<span style=
"color: #990000">|
</span>
808 t
<span style=
"color: #990000">.
</span>integer
<span style=
"color: #990000">:
</span>assembly_id
809 t
<span style=
"color: #990000">.
</span>integer
<span style=
"color: #990000">:
</span>part_id
810 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
811 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
813 <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>down
814 drop_table
<span style=
"color: #990000">:
</span>assemblies_parts
815 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
816 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
817 </tt></pre></div></div>
818 <h3 id=
"_controlling_association_scope">3.4. Controlling Association Scope
</h3>
819 <div class=
"para"><p>By default, associations look for objects only within the current module's scope. This can be important when you declare Active Record models within a module. For example:
</p></div>
820 <div class=
"listingblock">
821 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
823 http://www.lorenzobettini.it
824 http://www.gnu.org/software/src-highlite -->
825 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">module
</span></span> MyApplication
826 <span style=
"font-weight: bold"><span style=
"color: #0000FF">module
</span></span> Business
827 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Supplier
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
828 has_one
<span style=
"color: #990000">:
</span>account
829 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
831 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Account
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
832 belongs_to
<span style=
"color: #990000">:
</span>supplier
833 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
834 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
835 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
836 </tt></pre></div></div>
837 <div class=
"para"><p>This will work fine, because both the
<tt>Supplier
</tt> and the
<tt>Account
</tt> class are defined within the same scope. But this will not work, because
<tt>Supplier
</tt> and
<tt>Account
</tt> are defined in different scopes:
</p></div>
838 <div class=
"listingblock">
839 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
841 http://www.lorenzobettini.it
842 http://www.gnu.org/software/src-highlite -->
843 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">module
</span></span> MyApplication
844 <span style=
"font-weight: bold"><span style=
"color: #0000FF">module
</span></span> Business
845 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Supplier
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
846 has_one
<span style=
"color: #990000">:
</span>account
847 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
848 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
850 <span style=
"font-weight: bold"><span style=
"color: #0000FF">module
</span></span> Billing
851 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Account
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
852 belongs_to
<span style=
"color: #990000">:
</span>supplier
853 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
854 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
855 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
856 </tt></pre></div></div>
857 <div class=
"para"><p>To associate a model with a model in a different scope, you must specify the complete class name in your association declaration:
</p></div>
858 <div class=
"listingblock">
859 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
861 http://www.lorenzobettini.it
862 http://www.gnu.org/software/src-highlite -->
863 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">module
</span></span> MyApplication
864 <span style=
"font-weight: bold"><span style=
"color: #0000FF">module
</span></span> Business
865 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Supplier
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
866 has_one
<span style=
"color: #990000">:
</span>account
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>class_name
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"MyApplication::Billing::Account"</span>
867 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
868 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
870 <span style=
"font-weight: bold"><span style=
"color: #0000FF">module
</span></span> Billing
871 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Account
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
872 belongs_to
<span style=
"color: #990000">:
</span>supplier
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>class_name
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"MyApplication::Business::Supplier"</span>
873 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
874 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
875 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
876 </tt></pre></div></div>
878 <h2 id=
"_detailed_association_reference">4. Detailed Association Reference
</h2>
879 <div class=
"sectionbody">
880 <div class=
"para"><p>The following sections give the details of each type of association, including the methods that they add and the options that you can use when declaring an association.
</p></div>
881 <h3 id=
"_the_tt_belongs_to_tt_association_2">4.1. The
<tt>belongs_to
</tt> Association
</h3>
882 <div class=
"para"><p>The
<tt>belongs_to
</tt> association creates a one-to-one match with another model. In database terms, this association says that this class contains the foreign key. If the other class contains the foreign key, then you should use
<tt>has_one
</tt> instead.
</p></div>
883 <h4 id=
"_methods_added_by_tt_belongs_to_tt">4.1.1. Methods Added by
<tt>belongs_to
</tt></h4>
884 <div class=
"para"><p>When you declare a
<tt>belongs_to
</tt> assocation, the declaring class automatically gains five methods related to the association:
</p></div>
885 <div class=
"ilist"><ul>
888 <tt><em>association
</em>(force_reload = false)
</tt>
893 <tt><em>association
</em>=(associate)
</tt>
898 <tt><em>association
</em>.nil?
</tt>
903 <tt>build
<em>_association
</em>(attributes = {})
</tt>
908 <tt>create
<em>_association
</em>(attributes = {})
</tt>
912 <div class=
"para"><p>In all of these methods,
<tt><em>association
</em></tt> is replaced with the symbol passed as the first argument to
<tt>belongs_to
</tt>. For example, given the declaration:
</p></div>
913 <div class=
"listingblock">
914 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
916 http://www.lorenzobettini.it
917 http://www.gnu.org/software/src-highlite -->
918 <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
919 belongs_to
<span style=
"color: #990000">:
</span>customer
920 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
921 </tt></pre></div></div>
922 <div class=
"para"><p>Each instance of the order model will have these methods:
</p></div>
923 <div class=
"listingblock">
924 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
926 http://www.lorenzobettini.it
927 http://www.gnu.org/software/src-highlite -->
929 customer
<span style=
"color: #990000">=
</span>
930 customer
<span style=
"color: #990000">.
</span><span style=
"font-weight: bold"><span style=
"color: #0000FF">nil
</span></span><span style=
"color: #990000">?
</span>
933 </tt></pre></div></div>
934 <h5 id=
"_tt_em_association_em_force_reload_false_tt"><tt><em>association
</em>(force_reload = false)
</tt></h5>
935 <div class=
"para"><p>The
<tt><em>association
</em></tt> method returns the associated object, if any. If no associated object is found, it returns
<tt>nil
</tt>.
</p></div>
936 <div class=
"listingblock">
937 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
939 http://www.lorenzobettini.it
940 http://www.gnu.org/software/src-highlite -->
941 <pre><tt><span style=
"color: #009900">@customer
</span> <span style=
"color: #990000">=
</span> <span style=
"color: #009900">@order
</span><span style=
"color: #990000">.
</span>customer
942 </tt></pre></div></div>
943 <div class=
"para"><p>If the associated object has already been retrieved from the database for this object, the cached version will be returned. To override this behavior (and force a database read), pass
<tt>true
</tt> as the
<tt>force_reload
</tt> argument.
</p></div>
944 <h5 id=
"_tt_em_association_em_associate_tt"><tt><em>association
</em>=(associate)
</tt></h5>
945 <div class=
"para"><p>The
<tt><em>association
</em>=
</tt> method assigns an associated object to this object. Behind the scenes, this means extracting the primary key from the associate object and setting this object's foreign key to the same value.
</p></div>
946 <div class=
"listingblock">
947 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
949 http://www.lorenzobettini.it
950 http://www.gnu.org/software/src-highlite -->
951 <pre><tt><span style=
"color: #009900">@order
</span><span style=
"color: #990000">.
</span>customer
<span style=
"color: #990000">=
</span> <span style=
"color: #009900">@customer
</span>
952 </tt></pre></div></div>
953 <h5 id=
"_tt_em_association_em_nil_tt"><tt><em>association
</em>.nil?
</tt></h5>
954 <div class=
"para"><p>The
<tt><em>association
</em>.nil?
</tt> method returns
<tt>true
</tt> if there is no associated object.
</p></div>
955 <div class=
"listingblock">
956 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
958 http://www.lorenzobettini.it
959 http://www.gnu.org/software/src-highlite -->
960 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">if
</span></span> <span style=
"color: #009900">@order
</span><span style=
"color: #990000">.
</span>customer
<span style=
"color: #990000">.
</span><span style=
"font-weight: bold"><span style=
"color: #0000FF">nil
</span></span><span style=
"color: #990000">?
</span>
961 <span style=
"color: #009900">@msg
</span> <span style=
"color: #990000">=
</span> <span style=
"color: #FF0000">"No customer found for this order"</span>
962 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
963 </tt></pre></div></div>
964 <h5 id=
"_tt_build_em_association_em_attributes_tt"><tt>build
<em>_association
</em>(attributes = {})
</tt></h5>
965 <div class=
"para"><p>The
<tt>build
<em>_association
</em></tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through this object's foreign key will be set, but the associated object will _not_ yet be saved.
</p></div>
966 <div class=
"listingblock">
967 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
969 http://www.lorenzobettini.it
970 http://www.gnu.org/software/src-highlite -->
971 <pre><tt><span style=
"color: #009900">@customer
</span> <span style=
"color: #990000">=
</span> <span style=
"color: #009900">@order
</span><span style=
"color: #990000">.
</span>build_customer
<span style=
"color: #990000">(
</span><span style=
"color: #FF0000">{
</span><span style=
"color: #990000">:
</span>customer_number
<span style=
"color: #990000">=
></span> <span style=
"color: #993399">123</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>customer_name
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"John Doe"</span><span style=
"color: #FF0000">}
</span><span style=
"color: #990000">)
</span>
972 </tt></pre></div></div>
973 <h5 id=
"_tt_create_em_association_em_attributes_tt"><tt>create
<em>_association
</em>(attributes = {})
</tt></h5>
974 <div class=
"para"><p>The
<tt>create
<em>_association
</em></tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through this object's foreign key will be set. In addition, the associated object _will_ be saved (assuming that it passes any validations).
</p></div>
975 <div class=
"listingblock">
976 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
978 http://www.lorenzobettini.it
979 http://www.gnu.org/software/src-highlite -->
980 <pre><tt><span style=
"color: #009900">@customer
</span> <span style=
"color: #990000">=
</span> <span style=
"color: #009900">@order
</span><span style=
"color: #990000">.
</span>create_customer
<span style=
"color: #990000">(
</span><span style=
"color: #FF0000">{
</span><span style=
"color: #990000">:
</span>customer_number
<span style=
"color: #990000">=
></span> <span style=
"color: #993399">123</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>customer_name
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"John Doe"</span><span style=
"color: #FF0000">}
</span><span style=
"color: #990000">)
</span>
981 </tt></pre></div></div>
982 <h4 id=
"_options_for_tt_belongs_to_tt">4.1.2. Options for
<tt>belongs_to
</tt></h4>
983 <div class=
"para"><p>In many situations, you can use the default behavior of
<tt>belongs_to
</tt> without any customization. But despite Rails' emphasis of convention over customization, you can alter that behavior in a number of ways. This section covers the options that you can pass when you create a
<tt>belongs_to
</tt> association. For example, an association with several options might look like this:
</p></div>
984 <div class=
"listingblock">
985 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
987 http://www.lorenzobettini.it
988 http://www.gnu.org/software/src-highlite -->
989 <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
990 belongs_to
<span style=
"color: #990000">:
</span>customer
<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=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>conditions
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"active = 1"</span>
991 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
992 </tt></pre></div></div>
993 <div class=
"para"><p>The
<tt>belongs_to
</tt> association supports these options:
</p></div>
994 <div class=
"ilist"><ul>
1002 <tt>:conditions
</tt>
1007 <tt>:counter_cache
</tt>
1017 <tt>:foreign_key
</tt>
1027 <tt>:polymorphic
</tt>
1046 <h5 id=
"_tt_class_name_tt"><tt>:class_name
</tt></h5>
1047 <div class=
"para"><p>If the name of the other model cannot be derived from the association name, you can use the
<tt>:class_name
</tt> option to supply the model name. For example, if an order belongs to a customer, but the actual name of the model containing customers is
<tt>Patron
</tt>, you'd set things up this way:
</p></div>
1048 <div class=
"listingblock">
1049 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1051 http://www.lorenzobettini.it
1052 http://www.gnu.org/software/src-highlite -->
1053 <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
1054 belongs_to
<span style=
"color: #990000">:
</span>customer
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>class_name
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"Patron"</span>
1055 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1056 </tt></pre></div></div>
1057 <h5 id=
"_tt_conditions_tt"><tt>:conditions
</tt></h5>
1058 <div class=
"para"><p>The
<tt>:conditions
</tt> option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL
<tt>WHERE
</tt> clause).
</p></div>
1059 <div class=
"listingblock">
1060 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1062 http://www.lorenzobettini.it
1063 http://www.gnu.org/software/src-highlite -->
1064 <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
1065 belongs_to
<span style=
"color: #990000">:
</span>customer
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>conditions
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"active = 1"</span>
1066 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1067 </tt></pre></div></div>
1068 <h5 id=
"_tt_counter_cache_tt"><tt>:counter_cache
</tt></h5>
1069 <div class=
"para"><p>The
<tt>:counter_cache
</tt> option can be used to make finding the number of belonging objects more efficient. Consider these models:
</p></div>
1070 <div class=
"listingblock">
1071 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1073 http://www.lorenzobettini.it
1074 http://www.gnu.org/software/src-highlite -->
1075 <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
1076 belongs_to
<span style=
"color: #990000">:
</span>customer
1077 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1078 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Customer
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1079 has_many
<span style=
"color: #990000">:
</span>orders
1080 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1081 </tt></pre></div></div>
1082 <div class=
"para"><p>With these declarations, asking for the value of
<tt>@customer.orders.size
</tt> requires making a call to the database to perform a
<tt>COUNT(*)
</tt> query. To avoid this call, you can add a counter cache to the
<em>belonging
</em> model:
</p></div>
1083 <div class=
"listingblock">
1084 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1086 http://www.lorenzobettini.it
1087 http://www.gnu.org/software/src-highlite -->
1088 <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
1089 belongs_to
<span style=
"color: #990000">:
</span>customer
<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>
1090 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1091 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Customer
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1092 has_many
<span style=
"color: #990000">:
</span>orders
1093 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1094 </tt></pre></div></div>
1095 <div class=
"para"><p>With this declaration, Rails will keep the cache value up to date, and then return that value in response to the
<tt>.size
</tt> method.
</p></div>
1096 <div class=
"para"><p>Although the
<tt>:counter_cache
</tt> option is specified on the model that includes the
<tt>belongs_to
</tt> declaration, the actual column must be added to the
<em>associated
</em> model. In the case above, you would need to add a column named
<tt>orders_count
</tt> to the
<tt>Customer
</tt> model. You can override the default column name if you need to:
</p></div>
1097 <div class=
"listingblock">
1098 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1100 http://www.lorenzobettini.it
1101 http://www.gnu.org/software/src-highlite -->
1102 <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
1103 belongs_to
<span style=
"color: #990000">:
</span>customer
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>counter_cache
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">:
</span>count_of_orders
1104 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1105 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Customer
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1106 has_many
<span style=
"color: #990000">:
</span>orders
1107 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1108 </tt></pre></div></div>
1109 <div class=
"para"><p>Counter cache columns are added to the containing model's list of read-only attributes through
<tt>attr_readonly
</tt>.
</p></div>
1110 <h5 id=
"_tt_dependent_tt"><tt>:dependent
</tt></h5>
1111 <div class=
"para"><p>If you set the
<tt>:dependent
</tt> option to
<tt>:destroy
</tt>, then deleting this object will call the destroy method on the associated object to delete that object. If you set the
<tt>:dependent
</tt> option to
<tt>:delete
</tt>, then deleting this object will delete the associated object
<em>without
</em> calling its
<tt>destroy
</tt> method.
</p></div>
1112 <div class=
"admonitionblock">
1115 <img src=
"./images/icons/warning.png" alt=
"Warning" />
1117 <td class=
"content">You should not specify this option on a
<tt>belongs_to
</tt> association that is connected with a
<tt>has_many
</tt> association on the other class. Doing so can lead to orphaned records in your database.
</td>
1120 <h5 id=
"_tt_foreign_key_tt"><tt>:foreign_key
</tt></h5>
1121 <div class=
"para"><p>By convention, Rails guesses that the column used to hold the foreign key on this model is the name of the association with the suffix
<tt>_id
</tt> added. The
<tt>:foreign_key
</tt> option lets you set the name of the foreign key directly:
</p></div>
1122 <div class=
"listingblock">
1123 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1125 http://www.lorenzobettini.it
1126 http://www.gnu.org/software/src-highlite -->
1127 <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
1128 belongs_to
<span style=
"color: #990000">:
</span>customer
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>class_name
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"Patron"</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>foreign_key
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"patron_id"</span>
1129 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1130 </tt></pre></div></div>
1131 <div class=
"admonitionblock">
1134 <img src=
"./images/icons/tip.png" alt=
"Tip" />
1136 <td class=
"content">In any case, Rails will not create foreign key columns for you. You need to explicitly define them as part of your migrations.
</td>
1139 <h5 id=
"_tt_include_tt"><tt>:include
</tt></h5>
1140 <div class=
"para"><p>You can use the :include option to specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:
</p></div>
1141 <div class=
"listingblock">
1142 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1144 http://www.lorenzobettini.it
1145 http://www.gnu.org/software/src-highlite -->
1146 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> LineItem
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1147 belongs_to
<span style=
"color: #990000">:
</span>order
1148 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1149 <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
1150 belongs_to
<span style=
"color: #990000">:
</span>customer
1151 has_many
<span style=
"color: #990000">:
</span>line_items
1152 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1153 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Customer
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1154 has_many
<span style=
"color: #990000">:
</span>orders
1155 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1156 </tt></pre></div></div>
1157 <div class=
"para"><p>If you frequently retrieve customers directly from line items (
<tt>@line_item.order.customer
</tt>), then you can make your code somewhat more efficient by including customers in the association from line items to orders:
</p></div>
1158 <div class=
"listingblock">
1159 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1161 http://www.lorenzobettini.it
1162 http://www.gnu.org/software/src-highlite -->
1163 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> LineItem
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1164 belongs_to
<span style=
"color: #990000">:
</span>order
<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: #990000">:
</span>customer
1165 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1166 <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
1167 belongs_to
<span style=
"color: #990000">:
</span>customer
1168 has_many
<span style=
"color: #990000">:
</span>line_items
1169 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1170 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Customer
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1171 has_many
<span style=
"color: #990000">:
</span>orders
1172 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1173 </tt></pre></div></div>
1174 <div class=
"admonitionblock">
1177 <img src=
"./images/icons/note.png" alt=
"Note" />
1179 <td class=
"content">There's no need to use
<tt>:include
</tt> for immediate associations - that is, if you have
<tt>Order belongs_to :customer
</tt>, then the customer is eager-loaded automatically when it's needed.
</td>
1182 <h5 id=
"_tt_polymorphic_tt"><tt>:polymorphic
</tt></h5>
1183 <div class=
"para"><p>Passing
<tt>true
</tt> to the
<tt>:polymorphic
</tt> option indicates that this is a polymorphic association. Polymorphic associations were discussed in detail earlier in this guide.
</p></div>
1184 <h5 id=
"_tt_readonly_tt"><tt>:readonly
</tt></h5>
1185 <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>
1186 <h5 id=
"_tt_select_tt"><tt>:select
</tt></h5>
1187 <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>
1188 <div class=
"admonitionblock">
1191 <img src=
"./images/icons/tip.png" alt=
"Tip" />
1193 <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>
1196 <h5 id=
"_tt_validate_tt"><tt>:validate
</tt></h5>
1197 <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>
1198 <h4 id=
"_when_are_objects_saved">4.1.3. When are Objects Saved?
</h4>
1199 <div class=
"para"><p>Assigning an object to a
<tt>belongs_to
</tt> association does
<em>not
</em> automatically save the object. It does not save the associated object either.
</p></div>
1200 <h3 id=
"_the_has_one_association">4.2. The has_one Association
</h3>
1201 <div class=
"para"><p>The
<tt>has_one
</tt> association creates a one-to-one match with another model. In database terms, this association says that the other class contains the foreign key. If this class contains the foreign key, then you should use
<tt>belongs_to
</tt> instead.
</p></div>
1202 <h4 id=
"_methods_added_by_tt_has_one_tt">4.2.1. Methods Added by
<tt>has_one
</tt></h4>
1203 <div class=
"para"><p>When you declare a
<tt>has_one
</tt> association, the declaring class automatically gains five methods related to the association:
</p></div>
1204 <div class=
"ilist"><ul>
1207 <tt><em>association
</em>(force_reload = false)
</tt>
1212 <tt><em>association
</em>=(associate)
</tt>
1217 <tt><em>association
</em>.nil?
</tt>
1222 <tt>build
<em>_association
</em>(attributes = {})
</tt>
1227 <tt>create
<em>_association
</em>(attributes = {})
</tt>
1231 <div class=
"para"><p>In all of these methods,
<tt><em>association
</em></tt> is replaced with the symbol passed as the first argument to
<tt>has_one
</tt>. For example, given the declaration:
</p></div>
1232 <div class=
"listingblock">
1233 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1235 http://www.lorenzobettini.it
1236 http://www.gnu.org/software/src-highlite -->
1237 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Supplier
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1238 has_one
<span style=
"color: #990000">:
</span>account
1239 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1240 </tt></pre></div></div>
1241 <div class=
"para"><p>Each instance of the
<tt>Supplier
</tt> model will have these methods:
</p></div>
1242 <div class=
"listingblock">
1243 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1245 http://www.lorenzobettini.it
1246 http://www.gnu.org/software/src-highlite -->
1248 account
<span style=
"color: #990000">=
</span>
1249 account
<span style=
"color: #990000">.
</span><span style=
"font-weight: bold"><span style=
"color: #0000FF">nil
</span></span><span style=
"color: #990000">?
</span>
1252 </tt></pre></div></div>
1253 <h5 id=
"_tt_em_association_em_force_reload_false_tt_2"><tt><em>association
</em>(force_reload = false)
</tt></h5>
1254 <div class=
"para"><p>The
<tt><em>association
</em></tt> method returns the associated object, if any. If no associated object is found, it returns
<tt>nil
</tt>.
</p></div>
1255 <div class=
"listingblock">
1256 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1258 http://www.lorenzobettini.it
1259 http://www.gnu.org/software/src-highlite -->
1260 <pre><tt><span style=
"color: #009900">@account
</span> <span style=
"color: #990000">=
</span> <span style=
"color: #009900">@supplier
</span><span style=
"color: #990000">.
</span>account
1261 </tt></pre></div></div>
1262 <div class=
"para"><p>If the associated object has already been retrieved from the database for this object, the cached version will be returned. To override this behavior (and force a database read), pass
<tt>true
</tt> as the
<tt>force_reload
</tt> argument.
</p></div>
1263 <h5 id=
"_tt_em_association_em_associate_tt_2"><tt><em>association
</em>=(associate)
</tt></h5>
1264 <div class=
"para"><p>The
<tt><em>association
</em>=
</tt> method assigns an associated object to this object. Behind the scenes, this means extracting the primary key from this object and setting the associate object's foreign key to the same value.
</p></div>
1265 <div class=
"listingblock">
1266 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1268 http://www.lorenzobettini.it
1269 http://www.gnu.org/software/src-highlite -->
1270 <pre><tt><span style=
"color: #009900">@suppler
</span><span style=
"color: #990000">.
</span>account
<span style=
"color: #990000">=
</span> <span style=
"color: #009900">@account
</span>
1271 </tt></pre></div></div>
1272 <h5 id=
"_tt_em_association_em_nil_tt_2"><tt><em>association
</em>.nil?
</tt></h5>
1273 <div class=
"para"><p>The
<tt><em>association
</em>.nil?
</tt> method returns
<tt>true
</tt> if there is no associated object.
</p></div>
1274 <div class=
"listingblock">
1275 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1277 http://www.lorenzobettini.it
1278 http://www.gnu.org/software/src-highlite -->
1279 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">if
</span></span> <span style=
"color: #009900">@supplier
</span><span style=
"color: #990000">.
</span>account
<span style=
"color: #990000">.
</span><span style=
"font-weight: bold"><span style=
"color: #0000FF">nil
</span></span><span style=
"color: #990000">?
</span>
1280 <span style=
"color: #009900">@msg
</span> <span style=
"color: #990000">=
</span> <span style=
"color: #FF0000">"No account found for this supplier"</span>
1281 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1282 </tt></pre></div></div>
1283 <h5 id=
"_tt_build_em_association_em_attributes_tt_2"><tt>build
<em>_association
</em>(attributes = {})
</tt></h5>
1284 <div class=
"para"><p>The
<tt>build
<em>_association
</em></tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through its foreign key will be set, but the associated object will _not_ yet be saved.
</p></div>
1285 <div class=
"listingblock">
1286 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1288 http://www.lorenzobettini.it
1289 http://www.gnu.org/software/src-highlite -->
1290 <pre><tt><span style=
"color: #009900">@account
</span> <span style=
"color: #990000">=
</span> <span style=
"color: #009900">@supplier
</span><span style=
"color: #990000">.
</span>build_account
<span style=
"color: #990000">(
</span><span style=
"color: #FF0000">{
</span><span style=
"color: #990000">:
</span>terms
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"Net 30"</span><span style=
"color: #FF0000">}
</span><span style=
"color: #990000">)
</span>
1291 </tt></pre></div></div>
1292 <h5 id=
"_tt_create_em_association_em_attributes_tt_2"><tt>create
<em>_association
</em>(attributes = {})
</tt></h5>
1293 <div class=
"para"><p>The
<tt>create
<em>_association
</em></tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through its foreign key will be set. In addition, the associated object _will_ be saved (assuming that it passes any validations).
</p></div>
1294 <div class=
"listingblock">
1295 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1297 http://www.lorenzobettini.it
1298 http://www.gnu.org/software/src-highlite -->
1299 <pre><tt><span style=
"color: #009900">@account
</span> <span style=
"color: #990000">=
</span> <span style=
"color: #009900">@supplier
</span><span style=
"color: #990000">.
</span>create_account
<span style=
"color: #990000">(
</span><span style=
"color: #FF0000">{
</span><span style=
"color: #990000">:
</span>terms
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"Net 30"</span><span style=
"color: #FF0000">}
</span><span style=
"color: #990000">)
</span>
1300 </tt></pre></div></div>
1301 <h4 id=
"_options_for_tt_has_one_tt">4.2.2. Options for
<tt>has_one
</tt></h4>
1302 <div class=
"para"><p>In many situations, you can use the default behavior of
<tt>has_one
</tt> without any customization. But despite Rails' emphasis of convention over customization, you can alter that behavior in a number of ways. This section covers the options that you can pass when you create a
<tt>has_one
</tt> association. For example, an association with several options might look like this:
</p></div>
1303 <div class=
"listingblock">
1304 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1306 http://www.lorenzobettini.it
1307 http://www.gnu.org/software/src-highlite -->
1308 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Supplier
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1309 has_one
<span style=
"color: #990000">:
</span>account
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>class_name
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"Billing"</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>dependent
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">:
</span>nullify
1310 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1311 </tt></pre></div></div>
1312 <div class=
"para"><p>The
<tt>has_one
</tt> association supports these options:
</p></div>
1313 <div class=
"ilist"><ul>
1321 <tt>:class_name
</tt>
1326 <tt>:conditions
</tt>
1336 <tt>:foreign_key
</tt>
1351 <tt>:primary_key
</tt>
1371 <tt>:source_type
</tt>
1385 <h5 id=
"_tt_as_tt"><tt>:as
</tt></h5>
1386 <div class=
"para"><p>Setting the
<tt>:as
</tt> option indicates that this is a polymorphic association. Polymorphic associations are discussed in detail later in this guide.
</p></div>
1387 <h5 id=
"_tt_class_name_tt_2"><tt>:class_name
</tt></h5>
1388 <div class=
"para"><p>If the name of the other model cannot be derived from the association name, you can use the
<tt>:class_name
</tt> option to supply the model name. For example, if a supplier has an account, but the actual name of the model containing accounts is Billing, you'd set things up this way:
</p></div>
1389 <div class=
"listingblock">
1390 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1392 http://www.lorenzobettini.it
1393 http://www.gnu.org/software/src-highlite -->
1394 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Supplier
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1395 has_one
<span style=
"color: #990000">:
</span>account
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>class_name
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"Billing"</span>
1396 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1397 </tt></pre></div></div>
1398 <h5 id=
"_tt_conditions_tt_2"><tt>:conditions
</tt></h5>
1399 <div class=
"para"><p>The
<tt>:conditions
</tt> option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL
<tt>WHERE
</tt> clause).
</p></div>
1400 <div class=
"listingblock">
1401 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1403 http://www.lorenzobettini.it
1404 http://www.gnu.org/software/src-highlite -->
1405 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Supplier
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1406 has_one
<span style=
"color: #990000">:
</span>account
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>conditions
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"confirmed = 1"</span>
1407 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1408 </tt></pre></div></div>
1409 <h5 id=
"_tt_dependent_tt_2"><tt>:dependent
</tt></h5>
1410 <div class=
"para"><p>If you set the
<tt>:dependent
</tt> option to
<tt>:destroy
</tt>, then deleting this object will call the destroy method on the associated object to delete that object. If you set the
<tt>:dependent
</tt> option to
<tt>:delete
</tt>, then deleting this object will delete the associated object
<em>without
</em> calling its
<tt>destroy
</tt> method. If you set the
<tt>:dependent
</tt> option to
<tt>:nullify
</tt>, then deleting this object will set the foreign key in the association object to
<tt>NULL
</tt>.
</p></div>
1411 <h5 id=
"_tt_foreign_key_tt_2"><tt>:foreign_key
</tt></h5>
1412 <div class=
"para"><p>By convention, Rails guesses that the column used to hold the foreign key on the other model is the name of this model with the suffix
<tt>_id
</tt> added. The
<tt>:foreign_key
</tt> option lets you set the name of the foreign key directly:
</p></div>
1413 <div class=
"listingblock">
1414 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1416 http://www.lorenzobettini.it
1417 http://www.gnu.org/software/src-highlite -->
1418 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Supplier
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1419 has_one
<span style=
"color: #990000">:
</span>account
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>foreign_key
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"supp_id"</span>
1420 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1421 </tt></pre></div></div>
1422 <div class=
"admonitionblock">
1425 <img src=
"./images/icons/tip.png" alt=
"Tip" />
1427 <td class=
"content">In any case, Rails will not create foreign key columns for you. You need to explicitly define them as part of your migrations.
</td>
1430 <h5 id=
"_tt_include_tt_2"><tt>:include
</tt></h5>
1431 <div class=
"para"><p>You can use the :include option to specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:
</p></div>
1432 <div class=
"listingblock">
1433 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1435 http://www.lorenzobettini.it
1436 http://www.gnu.org/software/src-highlite -->
1437 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Supplier
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1438 has_one
<span style=
"color: #990000">:
</span>account
1439 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1440 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Account
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1441 belongs_to
<span style=
"color: #990000">:
</span>supplier
1442 belongs_to
<span style=
"color: #990000">:
</span>representative
1443 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1444 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Representative
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1445 has_many
<span style=
"color: #990000">:
</span>accounts
1446 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1447 </tt></pre></div></div>
1448 <div class=
"para"><p>If you frequently retrieve representatives directly from suppliers (
<tt>@supplier.account.representative
</tt>), then you can make your code somewhat more efficient by including representatives in the association from suppliers to accounts:
</p></div>
1449 <div class=
"listingblock">
1450 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1452 http://www.lorenzobettini.it
1453 http://www.gnu.org/software/src-highlite -->
1454 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Supplier
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1455 has_one
<span style=
"color: #990000">:
</span>account
<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: #990000">:
</span>representative
1456 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1457 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Account
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1458 belongs_to
<span style=
"color: #990000">:
</span>supplier
1459 belongs_to
<span style=
"color: #990000">:
</span>representative
1460 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1461 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Representative
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1462 has_many
<span style=
"color: #990000">:
</span>accounts
1463 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1464 </tt></pre></div></div>
1465 <h5 id=
"_tt_order_tt"><tt>:order
</tt></h5>
1466 <div class=
"para"><p>The
<tt>:order
</tt> option dictates the order in which associated objects will be received (in the syntax used by a SQL
<tt>ORDER BY
</tt> clause). Because a
<tt>has_one
</tt> association will only retrieve a single associated object, this option should not be needed.
</p></div>
1467 <h5 id=
"_tt_primary_key_tt"><tt>:primary_key
</tt></h5>
1468 <div class=
"para"><p>By convention, Rails guesses that the column used to hold the primary key of this model is
<tt>id
</tt>. You can override this and explicitly specify the primary key with the
<tt>:primary_key
</tt> option.
</p></div>
1469 <h5 id=
"_tt_readonly_tt_2"><tt>:readonly
</tt></h5>
1470 <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>
1471 <h5 id=
"_tt_select_tt_2"><tt>:select
</tt></h5>
1472 <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>
1473 <h5 id=
"_tt_source_tt"><tt>:source
</tt></h5>
1474 <div class=
"para"><p>The
<tt>:source
</tt> option specifies the source association name for a
<tt>has_one :through
</tt> association.
</p></div>
1475 <h5 id=
"_tt_source_type_tt"><tt>:source_type
</tt></h5>
1476 <div class=
"para"><p>The
<tt>:source_type
</tt> option specifies the source association type for a
<tt>has_one :through
</tt> association that proceeds through a polymorphic association.
</p></div>
1477 <h5 id=
"_tt_through_tt"><tt>:through
</tt></h5>
1478 <div class=
"para"><p>The
<tt>:through
</tt> option specifies a join model through which to perform the query.
<tt>has_one :through
</tt> associations are discussed in detail later in this guide.
</p></div>
1479 <h5 id=
"_tt_validate_tt_2"><tt>:validate
</tt></h5>
1480 <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>
1481 <h4 id=
"_when_are_objects_saved_2">4.2.3. When are Objects Saved?
</h4>
1482 <div class=
"para"><p>When you assign an object to a
<tt>has_one
</tt> association, that object is automatically saved (in order to update its foreign key). In addition, any object being replaced is also automatically saved, because its foreign key will change too.
</p></div>
1483 <div class=
"para"><p>If either of these saves fails due to validation errors, then the assignment statement returns
<tt>false
</tt> and the assignment itself is cancelled.
</p></div>
1484 <div class=
"para"><p>If the parent object (the one declaring the
<tt>has_one
</tt> association) is unsaved (that is,
<tt>new_record?
</tt> returns
<tt>true
</tt>) then the child objects are not saved.
</p></div>
1485 <div class=
"para"><p>If you want to assign an object to a
<tt>has_one
</tt> association without saving the object, use the
<tt>association.build
</tt> method.
</p></div>
1486 <h3 id=
"_the_has_many_association">4.3. The has_many Association
</h3>
1487 <div class=
"para"><p>The
<tt>has_many
</tt> association creates a one-to-many relationship with another model. In database terms, this association says that the other class will have a foreign key that refers to instances of this class.
</p></div>
1488 <h4 id=
"_methods_added">4.3.1. Methods Added
</h4>
1489 <div class=
"para"><p>When you declare a
<tt>has_many
</tt> association, the declaring class automatically gains
13 methods related to the association:
</p></div>
1490 <div class=
"ilist"><ul>
1493 <tt><em>collection
</em>(force_reload = false)
</tt>
1498 <tt><em>collection
</em><<(object,
…)
</tt>
1503 <tt><em>collection
</em>.delete(object,
…)
</tt>
1508 <tt><em>collection
</em>=objects
</tt>
1513 <tt><em>collection_singular
</em>_ids
</tt>
1518 <tt><em>collection_singular
</em>_ids=ids
</tt>
1523 <tt><em>collection
</em>.clear
</tt>
1528 <tt><em>collection
</em>.empty?
</tt>
1533 <tt><em>collection
</em>.size
</tt>
1538 <tt><em>collection
</em>.find(
…)
</tt>
1543 <tt><em>collection
</em>.exist?(
…)
</tt>
1548 <tt><em>collection
</em>.build(attributes = {},
…)
</tt>
1553 <tt><em>collection
</em>.create(attributes = {})
</tt>
1557 <div class=
"para"><p>In all of these methods,
<tt><em>collection
</em></tt> is replaced with the symbol passed as the first argument to
<tt>has_many
</tt>, and
<tt><em>collection_singular
</em></tt> is replaced with the singularized version of that symbol.. For example, given the declaration:
</p></div>
1558 <div class=
"listingblock">
1559 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1561 http://www.lorenzobettini.it
1562 http://www.gnu.org/software/src-highlite -->
1563 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Customer
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1564 has_many
<span style=
"color: #990000">:
</span>orders
1565 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1566 </tt></pre></div></div>
1567 <div class=
"para"><p>Each instance of the customer model will have these methods:
</p></div>
1568 <div class=
"listingblock">
1569 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1571 http://www.lorenzobettini.it
1572 http://www.gnu.org/software/src-highlite -->
1573 <pre><tt>orders
<span style=
"color: #990000">(
</span>force_reload
<span style=
"color: #990000">=
</span> <span style=
"font-weight: bold"><span style=
"color: #0000FF">false
</span></span><span style=
"color: #990000">)
</span>
1574 orders
<span style=
"color: #990000"><<(
</span>object
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">...)
</span>
1575 orders
<span style=
"color: #990000">.
</span>delete
<span style=
"color: #990000">(
</span>object
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">...)
</span>
1576 orders
<span style=
"color: #990000">=
</span>objects
1578 order_ids
<span style=
"color: #990000">=
</span>ids
1579 orders
<span style=
"color: #990000">.
</span>clear
1580 orders
<span style=
"color: #990000">.
</span>empty?
1581 orders
<span style=
"color: #990000">.
</span>size
1582 orders
<span style=
"color: #990000">.
</span>find
<span style=
"color: #990000">(...)
</span>
1583 orders
<span style=
"color: #990000">.
</span>exist?
<span style=
"color: #990000">(...)
</span>
1584 orders
<span style=
"color: #990000">.
</span>build
<span style=
"color: #990000">(
</span>attributes
<span style=
"color: #990000">=
</span> <span style=
"color: #FF0000">{}
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">...)
</span>
1585 orders
<span style=
"color: #990000">.
</span>create
<span style=
"color: #990000">(
</span>attributes
<span style=
"color: #990000">=
</span> <span style=
"color: #FF0000">{}
</span><span style=
"color: #990000">)
</span>
1586 </tt></pre></div></div>
1587 <h5 id=
"_tt_em_collection_em_force_reload_false_tt"><tt><em>collection
</em>(force_reload = false)
</tt></h5>
1588 <div class=
"para"><p>The
<tt><em>collection
</em></tt> method returns an array of all of the associated objects. If there are no associated objects, it returns an empty array.
</p></div>
1589 <div class=
"listingblock">
1590 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1592 http://www.lorenzobettini.it
1593 http://www.gnu.org/software/src-highlite -->
1594 <pre><tt><span style=
"color: #009900">@orders
</span> <span style=
"color: #990000">=
</span> <span style=
"color: #009900">@customer
</span><span style=
"color: #990000">.
</span>orders
1595 </tt></pre></div></div>
1596 <h5 id=
"_tt_em_collection_em_lt_lt_object_8230_tt"><tt><em>collection
</em><<(object,
…)
</tt></h5>
1597 <div class=
"para"><p>The
<tt><em>collection
</em><<</tt> method adds one or more objects to the collection by setting their foreign keys to the primary key of the calling model.
</p></div>
1598 <div class=
"listingblock">
1599 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1601 http://www.lorenzobettini.it
1602 http://www.gnu.org/software/src-highlite -->
1603 <pre><tt><span style=
"color: #009900">@customer
</span><span style=
"color: #990000">.
</span>orders
<span style=
"color: #990000"><<</span> <span style=
"color: #009900">@order1
</span>
1604 </tt></pre></div></div>
1605 <h5 id=
"_tt_em_collection_em_delete_object_8230_tt"><tt><em>collection
</em>.delete(object,
…)
</tt></h5>
1606 <div class=
"para"><p>The
<tt><em>collection
</em>.delete
</tt> method removes one or more objects from the collection by setting their foreign keys to
<tt>NULL
</tt>.
</p></div>
1607 <div class=
"listingblock">
1608 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1610 http://www.lorenzobettini.it
1611 http://www.gnu.org/software/src-highlite -->
1612 <pre><tt><span style=
"color: #009900">@customer
</span><span style=
"color: #990000">.
</span>orders
<span style=
"color: #990000">.
</span>delete
<span style=
"color: #990000">(
</span><span style=
"color: #009900">@order1
</span><span style=
"color: #990000">)
</span>
1613 </tt></pre></div></div>
1614 <div class=
"admonitionblock">
1617 <img src=
"./images/icons/warning.png" alt=
"Warning" />
1619 <td class=
"content">Objects will be in addition destroyed if they're associated with
<tt>:dependent
⇒ :destroy
</tt>, and deleted if they're associated with
<tt>:dependent
⇒ :delete_all
</tt>.
</td>
1622 <h5 id=
"_tt_em_collection_em_objects_tt"><tt><em>collection
</em>=objects
</tt></h5>
1623 <div class=
"para"><p>The
<tt><em>collection
</em>=
</tt> method makes the collection contain only the supplied objects, by adding and deleting as appropriate.
</p></div>
1624 <h5 id=
"_tt_em_collection_singular_em_ids_tt"><tt><em>collection_singular
</em>_ids
</tt></h5>
1625 <div class=
"para"><p>The
<tt><em>collection_singular
</em>_ids
</tt> method returns an array of the ids of the objects in the collection.
</p></div>
1626 <div class=
"listingblock">
1627 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1629 http://www.lorenzobettini.it
1630 http://www.gnu.org/software/src-highlite -->
1631 <pre><tt><span style=
"color: #009900">@order_ids
</span> <span style=
"color: #990000">=
</span> <span style=
"color: #009900">@customer
</span><span style=
"color: #990000">.
</span>order_ids
1632 </tt></pre></div></div>
1633 <h5 id=
"_tt_em_collection_singular_em_ids_ids_tt"><tt><em>_collection_singular
</em>_ids=ids
</tt></h5>
1634 <div class=
"para"><p>The
<tt><em>_collection_singular
</em>_ids=
</tt> method makes the collection contain only the objects identified by the supplied primary key values, by adding and deleting as appropriate.
</p></div>
1635 <h5 id=
"_tt_em_collection_em_clear_tt"><tt><em>collection
</em>.clear
</tt></h5>
1636 <div class=
"para"><p>The
<tt><em>collection
</em>.clear
</tt> method removes every object from the collection. This destroys the associated objects if they are associated with
<tt>:dependent
⇒ :destroy
</tt>, deletes them directly from the database if
<tt>:dependent
⇒ :delete_all
</tt>, and otherwise sets their foreign keys to
<tt>NULL
</tt>.
</p></div>
1637 <h5 id=
"_tt_em_collection_em_empty_tt"><tt><em>collection
</em>.empty?
</tt></h5>
1638 <div class=
"para"><p>The
<tt><em>collection
</em>.empty?
</tt> method returns
<tt>true
</tt> if the collection does not contain any associated objects.
</p></div>
1639 <div class=
"listingblock">
1640 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1642 http://www.lorenzobettini.it
1643 http://www.gnu.org/software/src-highlite -->
1644 <pre><tt><span style=
"color: #FF0000"><% if @customer.orders.empty? %
></span>
1646 <span style=
"color: #FF0000"><% end %
></span>
1647 </tt></pre></div></div>
1648 <h5 id=
"_tt_em_collection_em_size_tt"><tt><em>collection
</em>.size
</tt></h5>
1649 <div class=
"para"><p>The
<tt><em>collection
</em>.size
</tt> method returns the number of objects in the collection.
</p></div>
1650 <div class=
"listingblock">
1651 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1653 http://www.lorenzobettini.it
1654 http://www.gnu.org/software/src-highlite -->
1655 <pre><tt><span style=
"color: #009900">@order_count
</span> <span style=
"color: #990000">=
</span> <span style=
"color: #009900">@customer
</span><span style=
"color: #990000">.
</span>orders
<span style=
"color: #990000">.
</span>size
1656 </tt></pre></div></div>
1657 <h5 id=
"_tt_em_collection_em_find_8230_tt"><tt><em>collection
</em>.find(
…)
</tt></h5>
1658 <div class=
"para"><p>The
<tt><em>collection
</em>.find
</tt> method finds objects within the collection. It uses the same syntax and options as
<tt>ActiveRecord::Base.find
</tt>.
</p></div>
1659 <div class=
"listingblock">
1660 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1662 http://www.lorenzobettini.it
1663 http://www.gnu.org/software/src-highlite -->
1664 <pre><tt><span style=
"color: #009900">@open_orders
</span> <span style=
"color: #990000">=
</span> <span style=
"color: #009900">@customer
</span><span style=
"color: #990000">.
</span>orders
<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: #FF0000">"open = 1"</span><span style=
"color: #990000">)
</span>
1665 </tt></pre></div></div>
1666 <h5 id=
"_tt_em_collection_em_exist_8230_tt"><tt><em>collection
</em>.exist?(
…)
</tt></h5>
1667 <div class=
"para"><p>The
<tt><em>collection
</em>.exist?
</tt> method checks whether an object meeting the supplied conditions exists in the collection. It uses the same syntax and options as
<tt>ActiveRecord::Base.exists?
</tt>.
</p></div>
1668 <h5 id=
"_tt_em_collection_em_build_attributes_8230_tt"><tt><em>collection
</em>.build(attributes = {},
…)
</tt></h5>
1669 <div class=
"para"><p>The
<tt><em>collection
</em>.build
</tt> method returns one or more new objects of the associated type. These objects will be instantiated from the passed attributes, and the link through their foreign key will be created, but the associated objects will
<em>not
</em> yet be saved.
</p></div>
1670 <div class=
"listingblock">
1671 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1673 http://www.lorenzobettini.it
1674 http://www.gnu.org/software/src-highlite -->
1675 <pre><tt><span style=
"color: #009900">@order
</span> <span style=
"color: #990000">=
</span> <span style=
"color: #009900">@customer
</span><span style=
"color: #990000">.
</span>orders
<span style=
"color: #990000">.
</span>build
<span style=
"color: #990000">(
</span><span style=
"color: #FF0000">{
</span><span style=
"color: #990000">:
</span>order_date
<span style=
"color: #990000">=
></span> Time
<span style=
"color: #990000">.
</span>now
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>order_number
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"A12345"</span><span style=
"color: #FF0000">}
</span><span style=
"color: #990000">)
</span>
1676 </tt></pre></div></div>
1677 <h5 id=
"_tt_em_collection_em_create_attributes_tt"><tt><em>collection
</em>.create(attributes = {})
</tt></h5>
1678 <div class=
"para"><p>The
<tt><em>collection
</em>.create
</tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, the link through its foreign key will be created, and the associated object
<em>will
</em> be saved (assuming that it passes any validations).
</p></div>
1679 <div class=
"listingblock">
1680 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1682 http://www.lorenzobettini.it
1683 http://www.gnu.org/software/src-highlite -->
1684 <pre><tt><span style=
"color: #009900">@order
</span> <span style=
"color: #990000">=
</span> <span style=
"color: #009900">@customer
</span><span style=
"color: #990000">.
</span>orders
<span style=
"color: #990000">.
</span>create
<span style=
"color: #990000">(
</span><span style=
"color: #FF0000">{
</span><span style=
"color: #990000">:
</span>order_date
<span style=
"color: #990000">=
></span> Time
<span style=
"color: #990000">.
</span>now
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>order_number
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"A12345"</span><span style=
"color: #FF0000">}
</span><span style=
"color: #990000">)
</span>
1685 </tt></pre></div></div>
1686 <h4 id=
"_options_for_has_many">4.3.2. Options for has_many
</h4>
1687 <div class=
"para"><p>In many situations, you can use the default behavior for
<tt>has_many
</tt> without any customization. But you can alter that behavior in a number of ways. This section covers the options that you can pass when you create a
<tt>has_many
</tt> association. For example, an association with several options might look like this:
</p></div>
1688 <div class=
"listingblock">
1689 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1691 http://www.lorenzobettini.it
1692 http://www.gnu.org/software/src-highlite -->
1693 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Customer
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1694 has_many
<span style=
"color: #990000">:
</span>orders
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>dependent
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">:
</span>delete_all
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>validate
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">:
</span><span style=
"font-weight: bold"><span style=
"color: #0000FF">false
</span></span>
1695 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1696 </tt></pre></div></div>
1697 <div class=
"para"><p>The
<tt>has_many
</tt> association supports these options:
</p></div>
1698 <div class=
"ilist"><ul>
1706 <tt>:class_name
</tt>
1711 <tt>:conditions
</tt>
1716 <tt>:counter_sql
</tt>
1731 <tt>:finder_sql
</tt>
1736 <tt>:foreign_key
</tt>
1766 <tt>:primary_key
</tt>
1786 <tt>:source_type
</tt>
1805 <h5 id=
"_tt_as_tt_2"><tt>:as
</tt></h5>
1806 <div class=
"para"><p>Setting the
<tt>:as
</tt> option indicates that this is a polymorphic association, as discussed earlier in this guide.
</p></div>
1807 <h5 id=
"_tt_class_name_tt_3"><tt>:class_name
</tt></h5>
1808 <div class=
"para"><p>If the name of the other model cannot be derived from the association name, you can use the
<tt>:class_name
</tt> option to supply the model name. For example, if a customer has many orders, but the actual name of the model containing orders is
<tt>Transaction
</tt>, you'd set things up this way:
</p></div>
1809 <div class=
"listingblock">
1810 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1812 http://www.lorenzobettini.it
1813 http://www.gnu.org/software/src-highlite -->
1814 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Customer
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1815 has_many
<span style=
"color: #990000">:
</span>orders
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>class_name
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"Transaction"</span>
1816 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1817 </tt></pre></div></div>
1818 <h5 id=
"_tt_conditions_tt_3"><tt>:conditions
</tt></h5>
1819 <div class=
"para"><p>The
<tt>:conditions
</tt> option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL
<tt>WHERE
</tt> clause).
</p></div>
1820 <div class=
"listingblock">
1821 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1823 http://www.lorenzobettini.it
1824 http://www.gnu.org/software/src-highlite -->
1825 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Customer
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1826 has_many
<span style=
"color: #990000">:
</span>confirmed_orders
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>class_name
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"Order"</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>conditions
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"confirmed = 1"</span>
1827 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1828 </tt></pre></div></div>
1829 <div class=
"para"><p>You can also set conditions via a hash:
</p></div>
1830 <div class=
"listingblock">
1831 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1833 http://www.lorenzobettini.it
1834 http://www.gnu.org/software/src-highlite -->
1835 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Customer
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1836 has_many
<span style=
"color: #990000">:
</span>confirmed_orders
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>class_name
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"Order"</span><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>confirmed
<span style=
"color: #990000">=
></span> <span style=
"font-weight: bold"><span style=
"color: #0000FF">true
</span></span> <span style=
"color: #FF0000">}
</span>
1837 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1838 </tt></pre></div></div>
1839 <div class=
"para"><p>If you use a hash-style
<tt>:conditions
</tt> option, then record creation via this association will be automatically scoped using the hash. In this case, using
<tt>@customer.confirmed_orders.create
</tt> or
<tt>@customer.confirmed_orders.build
</tt> will create orders where the confirmed column has the value
<tt>true
</tt>.
</p></div>
1840 <h5 id=
"_tt_counter_sql_tt"><tt>:counter_sql
</tt></h5>
1841 <div class=
"para"><p>Normally Rails automatically generates the proper SQL to count the association members. With the
<tt>:counter_sql
</tt> option, you can specify a complete SQL statement to count them yourself.
</p></div>
1842 <div class=
"admonitionblock">
1845 <img src=
"./images/icons/note.png" alt=
"Note" />
1847 <td class=
"content">If you specify
<tt>:finder_sql
</tt> but not
<tt>:counter_sql
</tt>, then the counter SQL will be generated by substituting
<tt>SELECT COUNT(*) FROM
</tt> for the
<tt>SELECT
… FROM
</tt> clause of your
<tt>:finder_sql
</tt> statement.
</td>
1850 <h5 id=
"_tt_dependent_tt_3"><tt>:dependent
</tt></h5>
1851 <div class=
"para"><p>If you set the
<tt>:dependent
</tt> option to
<tt>:destroy
</tt>, then deleting this object will call the destroy method on the associated objects to delete those objects. If you set the
<tt>:dependent
</tt> option to
<tt>:delete_all
</tt>, then deleting this object will delete the associated objects
<em>without
</em> calling their
<tt>destroy
</tt> method. If you set the
<tt>:dependent
</tt> option to
<tt>:nullify
</tt>, then deleting this object will set the foreign key in the associated objects to
<tt>NULL
</tt>.
</p></div>
1852 <div class=
"admonitionblock">
1855 <img src=
"./images/icons/note.png" alt=
"Note" />
1857 <td class=
"content">This option is ignored when you use the
<tt>:through
</tt> option on the association.
</td>
1860 <h5 id=
"_tt_extend_tt"><tt>:extend
</tt></h5>
1861 <div class=
"para"><p>The
<tt>:extend
</tt> option specifies a named module to extend the association proxy. Association extensions are discussed in detail later in this guide.
</p></div>
1862 <h5 id=
"_tt_finder_sql_tt"><tt>:finder_sql
</tt></h5>
1863 <div class=
"para"><p>Normally Rails automatically generates the proper SQL to fetch the association members. With the
<tt>:finder_sql
</tt> option, you can specify a complete SQL statement to fetch them yourself. If fetching objects requires complex multi-table SQL, this may be necessary.
</p></div>
1864 <h5 id=
"_tt_foreign_key_tt_3"><tt>:foreign_key
</tt></h5>
1865 <div class=
"para"><p>By convention, Rails guesses that the column used to hold the foreign key on the other model is the name of this model with the suffix
<tt>_id
</tt> added. The
<tt>:foreign_key
</tt> option lets you set the name of the foreign key directly:
</p></div>
1866 <div class=
"listingblock">
1867 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1869 http://www.lorenzobettini.it
1870 http://www.gnu.org/software/src-highlite -->
1871 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Customer
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1872 has_many
<span style=
"color: #990000">:
</span>orders
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>foreign_key
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"cust_id"</span>
1873 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1874 </tt></pre></div></div>
1875 <div class=
"admonitionblock">
1878 <img src=
"./images/icons/tip.png" alt=
"Tip" />
1880 <td class=
"content">In any case, Rails will not create foreign key columns for you. You need to explicitly define them as part of your migrations.
</td>
1883 <h5 id=
"_tt_group_tt"><tt>:group
</tt></h5>
1884 <div class=
"para"><p>The
<tt>:group
</tt> option supplies an attribute name to group the result set by, using a
<tt>GROUP BY
</tt> clause in the finder SQL.
</p></div>
1885 <div class=
"listingblock">
1886 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1888 http://www.lorenzobettini.it
1889 http://www.gnu.org/software/src-highlite -->
1890 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Customer
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1891 has_many
<span style=
"color: #990000">:
</span>line_items
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>through
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">:
</span>orders
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>group
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"orders.id"</span>
1892 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1893 </tt></pre></div></div>
1894 <h5 id=
"_tt_include_tt_3"><tt>:include
</tt></h5>
1895 <div class=
"para"><p>You can use the :include option to specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:
</p></div>
1896 <div class=
"listingblock">
1897 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1899 http://www.lorenzobettini.it
1900 http://www.gnu.org/software/src-highlite -->
1901 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Customer
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1902 has_many
<span style=
"color: #990000">:
</span>orders
1903 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1904 <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
1905 belongs_to
<span style=
"color: #990000">:
</span>customer
1906 has_many
<span style=
"color: #990000">:
</span>line_items
1907 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1908 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> LineItem
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1909 belongs_to
<span style=
"color: #990000">:
</span>order
1910 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1911 </tt></pre></div></div>
1912 <div class=
"para"><p>If you frequently retrieve line items directly from customers (
<tt>@customer.orders.line_items
</tt>), then you can make your code somewhat more efficient by including line items in the association from customers to orders:
</p></div>
1913 <div class=
"listingblock">
1914 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1916 http://www.lorenzobettini.it
1917 http://www.gnu.org/software/src-highlite -->
1918 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Customer
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1919 has_many
<span style=
"color: #990000">:
</span>orders
<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: #990000">:
</span>line_items
1920 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1921 <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
1922 belongs_to
<span style=
"color: #990000">:
</span>customer
1923 has_many
<span style=
"color: #990000">:
</span>line_items
1924 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1925 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> LineItem
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1926 belongs_to
<span style=
"color: #990000">:
</span>order
1927 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1928 </tt></pre></div></div>
1929 <h5 id=
"_tt_limit_tt"><tt>:limit
</tt></h5>
1930 <div class=
"para"><p>The
<tt>:limit
</tt> option lets you restrict the total number of objects that will be fetched through an association.
</p></div>
1931 <div class=
"listingblock">
1932 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1934 http://www.lorenzobettini.it
1935 http://www.gnu.org/software/src-highlite -->
1936 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Customer
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1937 has_many
<span style=
"color: #990000">:
</span>recent_orders
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>class_name
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"Order"</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>order
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"order_date DESC"</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>limit
<span style=
"color: #990000">=
></span> <span style=
"color: #993399">100</span>
1938 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1939 </tt></pre></div></div>
1940 <h5 id=
"_tt_offset_tt"><tt>:offset
</tt></h5>
1941 <div class=
"para"><p>The
<tt>:offset
</tt> option lets you specify the starting offset for fetching objects via an association. For example, if you set
<tt>:offset
⇒ 11</tt>, it will skip the first
10 records.
</p></div>
1942 <h5 id=
"_tt_order_tt_2"><tt>:order
</tt></h5>
1943 <div class=
"para"><p>The
<tt>:order
</tt> option dictates the order in which associated objects will be received (in the syntax used by a SQL
<tt>ORDER BY
</tt> clause).
</p></div>
1944 <div class=
"listingblock">
1945 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
1947 http://www.lorenzobettini.it
1948 http://www.gnu.org/software/src-highlite -->
1949 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Customer
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
1950 has_many
<span style=
"color: #990000">:
</span>orders
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>order
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"date_confirmed DESC"</span>
1951 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
1952 </tt></pre></div></div>
1953 <h5 id=
"_tt_primary_key_tt_2"><tt>:primary_key
</tt></h5>
1954 <div class=
"para"><p>By convention, Rails guesses that the column used to hold the primary key of this model is
<tt>id
</tt>. You can override this and explicitly specify the primary key with the
<tt>:primary_key
</tt> option.
</p></div>
1955 <h5 id=
"_tt_readonly_tt_3"><tt>:readonly
</tt></h5>
1956 <div class=
"para"><p>If you set the
<tt>:readonly
</tt> option to
<tt>true
</tt>, then the associated objects will be read-only when retrieved via the association.
</p></div>
1957 <h5 id=
"_tt_select_tt_3"><tt>:select
</tt></h5>
1958 <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 objects. By default, Rails retrieves all columns.
</p></div>
1959 <div class=
"admonitionblock">
1962 <img src=
"./images/icons/warning.png" alt=
"Warning" />
1964 <td class=
"content">If you specify your own
<tt>:select
</tt>, be sure to include the primary key and foreign key columns of the associated model. If you do not, Rails will throw an error.
</td>
1967 <h5 id=
"_tt_source_tt_2"><tt>:source
</tt></h5>
1968 <div class=
"para"><p>The
<tt>:source
</tt> option specifies the source association name for a
<tt>has_many :through
</tt> association. You only need to use this option if the name of the source association cannot be automatically inferred from the association name.
</p></div>
1969 <h5 id=
"_tt_source_type_tt_2"><tt>:source_type
</tt></h5>
1970 <div class=
"para"><p>The
<tt>:source_type
</tt> option specifies the source association type for a
<tt>has_many :through
</tt> association that proceeds through a polymorphic association.
</p></div>
1971 <h5 id=
"_tt_through_tt_2"><tt>:through
</tt></h5>
1972 <div class=
"para"><p>The
<tt>:through
</tt> option specifies a join model through which to perform the query.
<tt>has_many :through
</tt> associations provide a way to implement many-to-many relationships, as discussed earlier in this guide.
</p></div>
1973 <h5 id=
"_tt_uniq_tt"><tt>:uniq
</tt></h5>
1974 <div class=
"para"><p>Specify the
<tt>:uniq
⇒ true
</tt> option to remove duplicates from the collection. This is most useful in conjunction with the
<tt>:through
</tt> option.
</p></div>
1975 <h5 id=
"_tt_validate_tt_3"><tt>:validate
</tt></h5>
1976 <div class=
"para"><p>If you set the
<tt>:validate
</tt> option to
<tt>false
</tt>, then associated objects will not be validated whenever you save this object. By default, this is
<tt>true
</tt>: associated objects will be validated when this object is saved.
</p></div>
1977 <h4 id=
"_when_are_objects_saved_3">4.3.3. When are Objects Saved?
</h4>
1978 <div class=
"para"><p>When you assign an object to a
<tt>has_many
</tt> association, that object is automatically saved (in order to update its foreign key). If you assign multiple objects in one statement, then they are all saved.
</p></div>
1979 <div class=
"para"><p>If any of these saves fails due to validation errors, then the assignment statement returns
<tt>false
</tt> and the assignment itself is cancelled.
</p></div>
1980 <div class=
"para"><p>If the parent object (the one declaring the
<tt>has_many
</tt> association) is unsaved (that is,
<tt>new_record?
</tt> returns
<tt>true
</tt>) then the child objects are not saved when they are added. All unsaved members of the association will automatically be saved when the parent is saved.
</p></div>
1981 <div class=
"para"><p>If you want to assign an object to a
<tt>has_many
</tt> association without saving the object, use the
<tt><em>collection
</em>.build
</tt> method.
</p></div>
1982 <h3 id=
"_the_tt_has_and_belongs_to_many_tt_association_2">4.4. The
<tt>has_and_belongs_to_many
</tt> Association
</h3>
1983 <div class=
"para"><p>The
<tt>has_and_belongs_to_many
</tt> association creates a many-to-many relationship with another model. In database terms, this associates two classes via an intermediate join table that includes foreign keys referring to each of the classes.
</p></div>
1984 <h4 id=
"_methods_added_2">4.4.1. Methods Added
</h4>
1985 <div class=
"para"><p>When you declare a
<tt>has_and_belongs_to_many
</tt> association, the declaring class automatically gains
13 methods related to the association:
</p></div>
1986 <div class=
"ilist"><ul>
1989 <tt><em>collection
</em>(force_reload = false)
</tt>
1994 <tt><em>collection
</em><<(object,
…)
</tt>
1999 <tt><em>collection
</em>.delete(object,
…)
</tt>
2004 <tt><em>collection
</em>=objects
</tt>
2009 <tt><em>collection_singular
</em>_ids
</tt>
2014 <tt><em>collection_singular
</em>_ids=ids
</tt>
2019 <tt><em>collection
</em>.clear
</tt>
2024 <tt><em>collection
</em>.empty?
</tt>
2029 <tt><em>collection
</em>.size
</tt>
2034 <tt><em>collection
</em>.find(
…)
</tt>
2039 <tt><em>collection
</em>.exist?(
…)
</tt>
2044 <tt><em>collection
</em>.build(attributes = {})
</tt>
2049 <tt><em>collection
</em>.create(attributes = {})
</tt>
2053 <div class=
"para"><p>In all of these methods,
<tt><em>collection
</em></tt> is replaced with the symbol passed as the first argument to
<tt>has_many
</tt>, and
<tt><em>collection
</em>_singular
</tt> is replaced with the singularized version of that symbol.. For example, given the declaration:
</p></div>
2054 <div class=
"listingblock">
2055 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2057 http://www.lorenzobettini.it
2058 http://www.gnu.org/software/src-highlite -->
2059 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Part
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
2060 has_and_belongs_to_many
<span style=
"color: #990000">:
</span>assemblies
2061 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
2062 </tt></pre></div></div>
2063 <div class=
"para"><p>Each instance of the part model will have these methods:
</p></div>
2064 <div class=
"listingblock">
2065 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2067 http://www.lorenzobettini.it
2068 http://www.gnu.org/software/src-highlite -->
2069 <pre><tt>assemblies
<span style=
"color: #990000">(
</span>force_reload
<span style=
"color: #990000">=
</span> <span style=
"font-weight: bold"><span style=
"color: #0000FF">false
</span></span><span style=
"color: #990000">)
</span>
2070 assemblies
<span style=
"color: #990000"><<(
</span>object
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">...)
</span>
2071 assemblies
<span style=
"color: #990000">.
</span>delete
<span style=
"color: #990000">(
</span>object
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">...)
</span>
2072 assemblies
<span style=
"color: #990000">=
</span>objects
2074 assembly_ids
<span style=
"color: #990000">=
</span>ids
2075 assemblies
<span style=
"color: #990000">.
</span>clear
2076 assemblies
<span style=
"color: #990000">.
</span>empty?
2077 assemblies
<span style=
"color: #990000">.
</span>size
2078 assemblies
<span style=
"color: #990000">.
</span>find
<span style=
"color: #990000">(...)
</span>
2079 assemblies
<span style=
"color: #990000">.
</span>exist?
<span style=
"color: #990000">(...)
</span>
2080 assemblies
<span style=
"color: #990000">.
</span>build
<span style=
"color: #990000">(
</span>attributes
<span style=
"color: #990000">=
</span> <span style=
"color: #FF0000">{}
</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">...)
</span>
2081 assemblies
<span style=
"color: #990000">.
</span>create
<span style=
"color: #990000">(
</span>attributes
<span style=
"color: #990000">=
</span> <span style=
"color: #FF0000">{}
</span><span style=
"color: #990000">)
</span>
2082 </tt></pre></div></div>
2083 <h5 id=
"_additional_column_methods">Additional Column Methods
</h5>
2084 <div class=
"para"><p>If the join table for a
<tt>has_and_belongs_to_many
</tt> association has additional columns beyond the two foreign keys, these columns will be added as attributes to records retrieved via that association. Records returned with additional attributes will always be read-only, because Rails cannot save changes to those attributes.
</p></div>
2085 <div class=
"admonitionblock">
2088 <img src=
"./images/icons/warning.png" alt=
"Warning" />
2090 <td class=
"content">The use of extra attributes on the join table in a
<tt>has_and_belongs_to_many
</tt> association is deprecated. If you require this sort of complex behavior on the table that joins two models in a many-to-many relationship, you should use a
<tt>has_many :through
</tt> association instead of
<tt>has_and_belongs_to_many
</tt>.
</td>
2093 <h5 id=
"_tt_em_collection_em_force_reload_false_tt_2"><tt><em>collection
</em>(force_reload = false)
</tt></h5>
2094 <div class=
"para"><p>The
<tt><em>collection
</em></tt> method returns an array of all of the associated objects. If there are no associated objects, it returns an empty array.
</p></div>
2095 <div class=
"listingblock">
2096 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2098 http://www.lorenzobettini.it
2099 http://www.gnu.org/software/src-highlite -->
2100 <pre><tt><span style=
"color: #009900">@assemblies
</span> <span style=
"color: #990000">=
</span> <span style=
"color: #009900">@part
</span><span style=
"color: #990000">.
</span>assemblies
2101 </tt></pre></div></div>
2102 <h5 id=
"_tt_em_collection_em_lt_lt_object_8230_tt_2"><tt><em>collection
</em><<(object,
…)
</tt></h5>
2103 <div class=
"para"><p>The
<tt><em>collection
</em><<</tt> method adds one or more objects to the collection by creating records in the join table.
</p></div>
2104 <div class=
"listingblock">
2105 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2107 http://www.lorenzobettini.it
2108 http://www.gnu.org/software/src-highlite -->
2109 <pre><tt><span style=
"color: #009900">@part
</span><span style=
"color: #990000">.
</span>assemblies
<span style=
"color: #990000"><<</span> <span style=
"color: #009900">@assembly1
</span>
2110 </tt></pre></div></div>
2111 <div class=
"admonitionblock">
2114 <img src=
"./images/icons/note.png" alt=
"Note" />
2116 <td class=
"content">This method is aliased as
<tt><em>collection
</em>.concat
</tt> and
<tt><em>collection
</em>.push
</tt>.
</td>
2119 <h5 id=
"_tt_em_collection_em_delete_object_8230_tt_2"><tt><em>collection
</em>.delete(object,
…)
</tt></h5>
2120 <div class=
"para"><p>The
<tt><em>collection
</em>.delete
</tt> method removes one or more objects from the collection by deleting records in the join table. This does not destroy the objects.
</p></div>
2121 <div class=
"listingblock">
2122 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2124 http://www.lorenzobettini.it
2125 http://www.gnu.org/software/src-highlite -->
2126 <pre><tt><span style=
"color: #009900">@part
</span><span style=
"color: #990000">.
</span>assemblies
<span style=
"color: #990000">.
</span>delete
<span style=
"color: #990000">(
</span><span style=
"color: #009900">@assembly1
</span><span style=
"color: #990000">)
</span>
2127 </tt></pre></div></div>
2128 <h5 id=
"_tt_em_collection_em_objects_tt_2"><tt><em>collection
</em>=objects
</tt></h5>
2129 <div class=
"para"><p>The
<tt><em>collection
</em>=
</tt> method makes the collection contain only the supplied objects, by adding and deleting as appropriate.
</p></div>
2130 <h5 id=
"_tt_em_collection_singular_em_ids_tt_2"><tt><em>collection_singular
</em>_ids
</tt></h5>
2131 <div class=
"para"><p># Returns an array of the associated objects' ids
</p></div>
2132 <div class=
"para"><p>The
<tt><em>collection_singular
</em>_ids
</tt> method returns an array of the ids of the objects in the collection.
</p></div>
2133 <div class=
"listingblock">
2134 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2136 http://www.lorenzobettini.it
2137 http://www.gnu.org/software/src-highlite -->
2138 <pre><tt><span style=
"color: #009900">@assembly_ids
</span> <span style=
"color: #990000">=
</span> <span style=
"color: #009900">@part
</span><span style=
"color: #990000">.
</span>assembly_ids
2139 </tt></pre></div></div>
2140 <h5 id=
"_tt_em_collection_singular_em_ids_ids_tt_2"><tt><em>collection_singular
</em>_ids=ids
</tt></h5>
2141 <div class=
"para"><p>The
<tt><em>collection_singular
</em>_ids=
</tt> method makes the collection contain only the objects identified by the supplied primary key values, by adding and deleting as appropriate.
</p></div>
2142 <h5 id=
"_tt_em_collection_em_clear_tt_2"><tt><em>collection
</em>.clear
</tt></h5>
2143 <div class=
"para"><p>The
<tt><em>collection
</em>.clear
</tt> method removes every object from the collection by deleting the rows from the joining tableassociation. This does not destroy the associated objects.
</p></div>
2144 <h5 id=
"_tt_em_collection_em_empty_tt_2"><tt><em>collection
</em>.empty?
</tt></h5>
2145 <div class=
"para"><p>The
<tt><em>collection
</em>.empty?
</tt> method returns
<tt>true
</tt> if the collection does not contain any associated objects.
</p></div>
2146 <div class=
"listingblock">
2147 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2149 http://www.lorenzobettini.it
2150 http://www.gnu.org/software/src-highlite -->
2151 <pre><tt><span style=
"color: #FF0000"><% if @part.assemblies.empty? %
></span>
2152 This part is
<span style=
"font-weight: bold"><span style=
"color: #0000FF">not
</span></span> used
<span style=
"font-weight: bold"><span style=
"color: #0000FF">in
</span></span> any assemblies
2153 <span style=
"color: #FF0000"><% end %
></span>
2154 </tt></pre></div></div>
2155 <h5 id=
"_tt_em_collection_em_size_tt_2"><tt><em>collection
</em>.size
</tt></h5>
2156 <div class=
"para"><p>The
<tt><em>collection
</em>.size
</tt> method returns the number of objects in the collection.
</p></div>
2157 <div class=
"listingblock">
2158 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2160 http://www.lorenzobettini.it
2161 http://www.gnu.org/software/src-highlite -->
2162 <pre><tt><span style=
"color: #009900">@assembly_count
</span> <span style=
"color: #990000">=
</span> <span style=
"color: #009900">@part
</span><span style=
"color: #990000">.
</span>assemblies
<span style=
"color: #990000">.
</span>size
2163 </tt></pre></div></div>
2164 <h5 id=
"_tt_em_collection_em_find_8230_tt_2"><tt><em>collection
</em>.find(
…)
</tt></h5>
2165 <div class=
"para"><p>The
<tt><em>collection
</em>.find
</tt> method finds objects within the collection. It uses the same syntax and options as
<tt>ActiveRecord::Base.find
</tt>. It also adds the additional condition that the object must be in the collection.
</p></div>
2166 <div class=
"listingblock">
2167 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2169 http://www.lorenzobettini.it
2170 http://www.gnu.org/software/src-highlite -->
2171 <pre><tt><span style=
"color: #009900">@new_assemblies
</span> <span style=
"color: #990000">=
</span> <span style=
"color: #009900">@part
</span><span style=
"color: #990000">.
</span>assemblies
<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 > ?"</span><span style=
"color: #990000">,
</span> <span style=
"color: #993399">2</span><span style=
"color: #990000">.
</span>days
<span style=
"color: #990000">.
</span>ago
<span style=
"color: #990000">])
</span>
2172 </tt></pre></div></div>
2173 <h5 id=
"_tt_em_collection_em_exist_8230_tt_2"><tt><em>collection
</em>.exist?(
…)
</tt></h5>
2174 <div class=
"para"><p>The
<tt><em>collection
</em>.exist?
</tt> method checks whether an object meeting the supplied conditions exists in the collection. It uses the same syntax and options as
<tt>ActiveRecord::Base.exists?
</tt>.
</p></div>
2175 <h5 id=
"_tt_em_collection_em_build_attributes_tt"><tt><em>collection
</em>.build(attributes = {})
</tt></h5>
2176 <div class=
"para"><p>The
<tt><em>collection
</em>.build
</tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through the join table will be created, but the associated object will
<em>not
</em> yet be saved.
</p></div>
2177 <div class=
"listingblock">
2178 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2180 http://www.lorenzobettini.it
2181 http://www.gnu.org/software/src-highlite -->
2182 <pre><tt><span style=
"color: #009900">@assembly
</span> <span style=
"color: #990000">=
</span> <span style=
"color: #009900">@part
</span><span style=
"color: #990000">.
</span>assemblies
<span style=
"color: #990000">.
</span>build
<span style=
"color: #990000">(
</span><span style=
"color: #FF0000">{
</span><span style=
"color: #990000">:
</span>assembly_name
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"Transmission housing"</span><span style=
"color: #FF0000">}
</span><span style=
"color: #990000">)
</span>
2183 </tt></pre></div></div>
2184 <h5 id=
"_tt_em_collection_em_create_attributes_tt_2"><tt><em>collection
</em>.create(attributes = {})
</tt></h5>
2185 <div class=
"para"><p>The
<tt><em>collection
</em>.create
</tt> method returns a new object of the associated type. This objects will be instantiated from the passed attributes, the link through the join table will be created, and the associated object
<em>will
</em> be saved (assuming that it passes any validations).
</p></div>
2186 <div class=
"listingblock">
2187 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2189 http://www.lorenzobettini.it
2190 http://www.gnu.org/software/src-highlite -->
2191 <pre><tt><span style=
"color: #009900">@assembly
</span> <span style=
"color: #990000">=
</span> <span style=
"color: #009900">@part
</span><span style=
"color: #990000">.
</span>assemblies
<span style=
"color: #990000">.
</span>create
<span style=
"color: #990000">(
</span><span style=
"color: #FF0000">{
</span><span style=
"color: #990000">:
</span>assembly_name
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"Transmission housing"</span><span style=
"color: #FF0000">}
</span><span style=
"color: #990000">)
</span>
2192 </tt></pre></div></div>
2193 <h4 id=
"_options_for_has_and_belongs_to_many">4.4.2. Options for has_and_belongs_to_many
</h4>
2194 <div class=
"para"><p>In many situations, you can use the default behavior for
<tt>has_and_belongs_to_many
</tt> without any customization. But you can alter that behavior in a number of ways. This section cover the options that you can pass when you create a
<tt>has_and_belongs_to_many
</tt> association. For example, an association with several options might look like this:
</p></div>
2195 <div class=
"listingblock">
2196 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2198 http://www.lorenzobettini.it
2199 http://www.gnu.org/software/src-highlite -->
2200 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Parts
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
2201 has_and_belongs_to_many
<span style=
"color: #990000">:
</span>assemblies
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>uniq
<span style=
"color: #990000">=
></span> <span style=
"font-weight: bold"><span style=
"color: #0000FF">true
</span></span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>read_only
<span style=
"color: #990000">=
></span> <span style=
"font-weight: bold"><span style=
"color: #0000FF">true
</span></span>
2202 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
2203 </tt></pre></div></div>
2204 <div class=
"para"><p>The
<tt>has_and_belongs_to_many
</tt> association supports these options:
</p></div>
2205 <div class=
"ilist"><ul>
2208 <tt>:association_foreign_key
</tt>
2213 <tt>:class_name
</tt>
2218 <tt>:conditions
</tt>
2223 <tt>:counter_sql
</tt>
2228 <tt>:delete_sql
</tt>
2238 <tt>:finder_sql
</tt>
2243 <tt>:foreign_key
</tt>
2258 <tt>:insert_sql
</tt>
2263 <tt>:join_table
</tt>
2302 <h5 id=
"_tt_association_foreign_key_tt"><tt>:association_foreign_key
</tt></h5>
2303 <div class=
"para"><p>By convention, Rails guesses that the column in the join table used to hold the foreign key pointing to the other model is the name of that model with the suffix
<tt>_id
</tt> added. The
<tt>:association_foreign_key
</tt> option lets you set the name of the foreign key directly:
</p></div>
2304 <div class=
"admonitionblock">
2307 <img src=
"./images/icons/tip.png" alt=
"Tip" />
2309 <td class=
"content">The
<tt>:foreign_key
</tt> and
<tt>:association_foreign_key
</tt> options are useful when setting up a many-to-many self-join. For example:
</td>
2312 <div class=
"listingblock">
2313 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2315 http://www.lorenzobettini.it
2316 http://www.gnu.org/software/src-highlite -->
2317 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> User
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
2318 has_and_belongs_to_many
<span style=
"color: #990000">:
</span>friends
<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=
"color: #990000">,
</span>
2319 <span style=
"color: #990000">:
</span>foreign_key
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"this_user_id"</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>association_foreign_key
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"other_user_id"</span>
2320 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
2321 </tt></pre></div></div>
2322 <h5 id=
"_tt_class_name_tt_4"><tt>:class_name
</tt></h5>
2323 <div class=
"para"><p>If the name of the other model cannot be derived from the association name, you can use the
<tt>:class_name
</tt> option to supply the model name. For example, if a part has many assemblies, but the actual name of the model containing assemblies is
<tt>Gadget
</tt>, you'd set things up this way:
</p></div>
2324 <div class=
"listingblock">
2325 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2327 http://www.lorenzobettini.it
2328 http://www.gnu.org/software/src-highlite -->
2329 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Parts
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
2330 has_and_belongs_to_many
<span style=
"color: #990000">:
</span>assemblies
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>class_name
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"Gadget"</span>
2331 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
2332 </tt></pre></div></div>
2333 <h5 id=
"_tt_conditions_tt_4"><tt>:conditions
</tt></h5>
2334 <div class=
"para"><p>The
<tt>:conditions
</tt> option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL
<tt>WHERE
</tt> clause).
</p></div>
2335 <div class=
"listingblock">
2336 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2338 http://www.lorenzobettini.it
2339 http://www.gnu.org/software/src-highlite -->
2340 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Parts
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
2341 has_and_belongs_to_many
<span style=
"color: #990000">:
</span>assemblies
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>conditions
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"factory = 'Seattle'"</span>
2342 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
2343 </tt></pre></div></div>
2344 <div class=
"para"><p>You can also set conditions via a hash:
</p></div>
2345 <div class=
"listingblock">
2346 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2348 http://www.lorenzobettini.it
2349 http://www.gnu.org/software/src-highlite -->
2350 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Parts
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
2351 has_and_belongs_to_many
<span style=
"color: #990000">:
</span>assemblies
<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>factory
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">'Seattle'
</span> <span style=
"color: #FF0000">}
</span>
2352 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
2353 </tt></pre></div></div>
2354 <div class=
"para"><p>If you use a hash-style
<tt>:conditions
</tt> option, then record creation via this association will be automatically scoped using the hash. In this case, using
<tt>@parts.assemblies.create
</tt> or
<tt>@parts.assemblies.build
</tt> will create orders where the factory column has the value
"Seattle".
</p></div>
2355 <h5 id=
"_tt_counter_sql_tt_2"><tt>:counter_sql
</tt></h5>
2356 <div class=
"para"><p>Normally Rails automatically generates the proper SQL to count the association members. With the
<tt>:counter_sql
</tt> option, you can specify a complete SQL statement to count them yourself.
</p></div>
2357 <div class=
"admonitionblock">
2360 <img src=
"./images/icons/note.png" alt=
"Note" />
2362 <td class=
"content">If you specify
<tt>:finder_sql
</tt> but not
<tt>:counter_sql
</tt>, then the counter SQL will be generated by substituting
<tt>SELECT COUNT(*) FROM
</tt> for the
<tt>SELECT
… FROM
</tt> clause of your
<tt>:finder_sql
</tt> statement.
</td>
2365 <h5 id=
"_tt_delete_sql_tt"><tt>:delete_sql
</tt></h5>
2366 <div class=
"para"><p>Normally Rails automatically generates the proper SQL to remove links between the associated classes. With the
<tt>:delete_sql
</tt> option, you can specify a complete SQL statement to delete them yourself.
</p></div>
2367 <h5 id=
"_tt_extend_tt_2"><tt>:extend
</tt></h5>
2368 <div class=
"para"><p>The
<tt>:extend
</tt> option specifies a named module to extend the association proxy. Association extensions are discussed in detail later in this guide.
</p></div>
2369 <h5 id=
"_tt_finder_sql_tt_2"><tt>:finder_sql
</tt></h5>
2370 <div class=
"para"><p>Normally Rails automatically generates the proper SQL to fetch the association members. With the
<tt>:finder_sql
</tt> option, you can specify a complete SQL statement to fetch them yourself. If fetching objects requires complex multi-table SQL, this may be necessary.
</p></div>
2371 <h5 id=
"_tt_foreign_key_tt_4"><tt>:foreign_key
</tt></h5>
2372 <div class=
"para"><p>By convention, Rails guesses that the column in the join table used to hold the foreign key pointing to this model is the name of this model with the suffix
<tt>_id
</tt> added. The
<tt>:foreign_key
</tt> option lets you set the name of the foreign key directly:
</p></div>
2373 <div class=
"listingblock">
2374 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2376 http://www.lorenzobettini.it
2377 http://www.gnu.org/software/src-highlite -->
2378 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> User
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
2379 has_and_belongs_to_many
<span style=
"color: #990000">:
</span>friends
<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=
"color: #990000">,
</span>
2380 <span style=
"color: #990000">:
</span>foreign_key
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"this_user_id"</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>association_foreign_key
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"other_user_id"</span>
2381 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
2382 </tt></pre></div></div>
2383 <h5 id=
"_tt_group_tt_2"><tt>:group
</tt></h5>
2384 <div class=
"para"><p>The
<tt>:group
</tt> option supplies an attribute name to group the result set by, using a
<tt>GROUP BY
</tt> clause in the finder SQL.
</p></div>
2385 <div class=
"listingblock">
2386 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2388 http://www.lorenzobettini.it
2389 http://www.gnu.org/software/src-highlite -->
2390 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Parts
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
2391 has_and_belongs_to_many
<span style=
"color: #990000">:
</span>assemblies
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>group
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"factory"</span>
2392 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
2393 </tt></pre></div></div>
2394 <h5 id=
"_tt_include_tt_4"><tt>:include
</tt></h5>
2395 <div class=
"para"><p>You can use the :include option to specify second-order associations that should be eager-loaded when this association is used.
</p></div>
2396 <h5 id=
"_tt_insert_sql_tt"><tt>:insert_sql
</tt></h5>
2397 <div class=
"para"><p>Normally Rails automatically generates the proper SQL to create links between the associated classes. With the
<tt>:insert_sql
</tt> option, you can specify a complete SQL statement to insert them yourself.
</p></div>
2398 <h5 id=
"_tt_join_table_tt"><tt>:join_table
</tt></h5>
2399 <div class=
"para"><p>If the default name of the join table, based on lexical ordering, is not what you want, you can use the
<tt>:join_table
</tt> option to override the default.
</p></div>
2400 <h5 id=
"_tt_limit_tt_2"><tt>:limit
</tt></h5>
2401 <div class=
"para"><p>The
<tt>:limit
</tt> option lets you restrict the total number of objects that will be fetched through an association.
</p></div>
2402 <div class=
"listingblock">
2403 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2405 http://www.lorenzobettini.it
2406 http://www.gnu.org/software/src-highlite -->
2407 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Parts
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
2408 has_and_belongs_to_many
<span style=
"color: #990000">:
</span>assemblies
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>order
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"created_at DESC"</span><span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>limit
<span style=
"color: #990000">=
></span> <span style=
"color: #993399">50</span>
2409 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
2410 </tt></pre></div></div>
2411 <h5 id=
"_tt_offset_tt_2"><tt>:offset
</tt></h5>
2412 <div class=
"para"><p>The
<tt>:offset
</tt> option lets you specify the starting offset for fetching objects via an association. For example, if you set
<tt>:offset
⇒ 11</tt>, it will skip the first
10 records.
</p></div>
2413 <h5 id=
"_tt_order_tt_3"><tt>:order
</tt></h5>
2414 <div class=
"para"><p>The
<tt>:order
</tt> option dictates the order in which associated objects will be received (in the syntax used by a SQL
<tt>ORDER BY
</tt> clause).
</p></div>
2415 <div class=
"listingblock">
2416 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2418 http://www.lorenzobettini.it
2419 http://www.gnu.org/software/src-highlite -->
2420 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Parts
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
2421 has_and_belongs_to_many
<span style=
"color: #990000">:
</span>assemblies
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>order
<span style=
"color: #990000">=
></span> <span style=
"color: #FF0000">"assembly_name ASC"</span>
2422 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
2423 </tt></pre></div></div>
2424 <h5 id=
"_tt_readonly_tt_4"><tt>:readonly
</tt></h5>
2425 <div class=
"para"><p>If you set the
<tt>:readonly
</tt> option to
<tt>true
</tt>, then the associated objects will be read-only when retrieved via the association.
</p></div>
2426 <h5 id=
"_tt_select_tt_4"><tt>:select
</tt></h5>
2427 <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 objects. By default, Rails retrieves all columns.
</p></div>
2428 <h5 id=
"_tt_uniq_tt_2"><tt>:uniq
</tt></h5>
2429 <div class=
"para"><p>Specify the
<tt>:uniq
⇒ true
</tt> option to remove duplicates from the collection.
</p></div>
2430 <h5 id=
"_tt_validate_tt_4"><tt>:validate
</tt></h5>
2431 <div class=
"para"><p>If you set the
<tt>:validate
</tt> option to
<tt>false
</tt>, then associated objects will not be validated whenever you save this object. By default, this is
<tt>true
</tt>: associated objects will be validated when this object is saved.
</p></div>
2432 <h4 id=
"_when_are_objects_saved_4">4.4.3. When are Objects Saved?
</h4>
2433 <div class=
"para"><p>When you assign an object to a
<tt>has_and_belongs_to_many
</tt> association, that object is automatically saved (in order to update the join table). If you assign multiple objects in one statement, then they are all saved.
</p></div>
2434 <div class=
"para"><p>If any of these saves fails due to validation errors, then the assignment statement returns
<tt>false
</tt> and the assignment itself is cancelled.
</p></div>
2435 <div class=
"para"><p>If the parent object (the one declaring the
<tt>has_and_belongs_to_many
</tt> association) is unsaved (that is,
<tt>new_record?
</tt> returns
<tt>true
</tt>) then the child objects are not saved when they are added. All unsaved members of the association will automatically be saved when the parent is saved.
</p></div>
2436 <div class=
"para"><p>If you want to assign an object to a
<tt>has_and_belongs_to_many
</tt> association without saving the object, use the
<tt><em>collection
</em>.build
</tt> method.
</p></div>
2437 <h3 id=
"_association_callbacks">4.5. Association Callbacks
</h3>
2438 <div class=
"para"><p>Normal callbacks hook into the lifecycle of Active Record objects, allowing you to work with those objects at various points. For example, you can use a
<tt>:before_save
</tt> callback to cause something to happen just before an object is saved.
</p></div>
2439 <div class=
"para"><p>Association callbacks are similar to normal callbacks, but they are triggered by events in the lifecycle of a collection. There are four available association callbacks:
</p></div>
2440 <div class=
"ilist"><ul>
2453 <tt>before_remove
</tt>
2458 <tt>after_remove
</tt>
2462 <div class=
"para"><p>You define association callbacks by adding options to the association declaration. For example:
</p></div>
2463 <div class=
"listingblock">
2464 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2466 http://www.lorenzobettini.it
2467 http://www.gnu.org/software/src-highlite -->
2468 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Customer
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
2469 has_many
<span style=
"color: #990000">:
</span>orders
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>before_add
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">:
</span>check_credit_limit
2471 <span style=
"font-weight: bold"><span style=
"color: #0000FF">def
</span></span> check_credit_limit
<span style=
"color: #990000">(
</span>order
<span style=
"color: #990000">)
</span>
2472 <span style=
"color: #990000">...
</span>
2473 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
2474 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
2475 </tt></pre></div></div>
2476 <div class=
"para"><p>Rails passes the object being added or removed to the callback.
</p></div>
2477 <div class=
"para"><p>You can stack callbacks on a single event by passing them as an array:
</p></div>
2478 <div class=
"listingblock">
2479 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2481 http://www.lorenzobettini.it
2482 http://www.gnu.org/software/src-highlite -->
2483 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Customer
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
2484 has_many
<span style=
"color: #990000">:
</span>orders
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>before_add
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">[:
</span>check_credit_limit
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>calculate_shipping_charges
<span style=
"color: #990000">]
</span>
2486 <span style=
"font-weight: bold"><span style=
"color: #0000FF">def
</span></span> check_credit_limit
<span style=
"color: #990000">(
</span>order
<span style=
"color: #990000">)
</span>
2487 <span style=
"color: #990000">...
</span>
2488 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
2490 <span style=
"font-weight: bold"><span style=
"color: #0000FF">def
</span></span> calculate_shipping_charges
<span style=
"color: #990000">(
</span>order
<span style=
"color: #990000">)
</span>
2491 <span style=
"color: #990000">...
</span>
2492 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
2493 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
2494 </tt></pre></div></div>
2495 <div class=
"para"><p>If a
<tt>before_add
</tt> callback throws an exception, the object does not get added to the collection. Similarly, if a
<tt>before_remove
</tt> callback throws an exception, the object does not get removed from the collection.
</p></div>
2496 <h3 id=
"_association_extensions">4.6. Association Extensions
</h3>
2497 <div class=
"para"><p>You're not limited to the functionality that Rails automatically builds into association proxy objects. You can also extend these objects through anonymous modules, adding new finders, creators, or other methods. For example:
</p></div>
2498 <div class=
"listingblock">
2499 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2501 http://www.lorenzobettini.it
2502 http://www.gnu.org/software/src-highlite -->
2503 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Customer
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
2504 has_many
<span style=
"color: #990000">:
</span>orders
<span style=
"font-weight: bold"><span style=
"color: #0000FF">do
</span></span>
2505 <span style=
"font-weight: bold"><span style=
"color: #0000FF">def
</span></span> find_by_order_prefix
<span style=
"color: #990000">(
</span>order_number
<span style=
"color: #990000">)
</span>
2506 find_by_region_id
<span style=
"color: #990000">(
</span>order_number
<span style=
"color: #990000">[
</span><span style=
"color: #993399">0</span><span style=
"color: #990000">..
</span><span style=
"color: #993399">2</span><span style=
"color: #990000">])
</span>
2507 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
2508 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
2509 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
2510 </tt></pre></div></div>
2511 <div class=
"para"><p>If you have an extension that should be shared by many associations, you can use a named extension module. For example:
</p></div>
2512 <div class=
"listingblock">
2513 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2515 http://www.lorenzobettini.it
2516 http://www.gnu.org/software/src-highlite -->
2517 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">module
</span></span> FindRecentExtension
2518 <span style=
"font-weight: bold"><span style=
"color: #0000FF">def
</span></span> find_recent
2519 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 > ?"</span><span style=
"color: #990000">,
</span> <span style=
"color: #993399">5</span><span style=
"color: #990000">.
</span>days
<span style=
"color: #990000">.
</span>ago
<span style=
"color: #990000">])
</span>
2520 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
2521 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
2523 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Customer
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
2524 has_many
<span style=
"color: #990000">:
</span>orders
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>extend
<span style=
"color: #990000">=
></span> FindRecentExtension
2525 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
2527 <span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Supplier
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
2528 has_many
<span style=
"color: #990000">:
</span>deliveries
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>extend
<span style=
"color: #990000">=
></span> FindRecentExtension
2529 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
2530 </tt></pre></div></div>
2531 <div class=
"para"><p>To include more than one extension module in a single association, specify an array of names:
</p></div>
2532 <div class=
"listingblock">
2533 <div class=
"content"><!-- Generator: GNU source-highlight 2.9
2535 http://www.lorenzobettini.it
2536 http://www.gnu.org/software/src-highlite -->
2537 <pre><tt><span style=
"font-weight: bold"><span style=
"color: #0000FF">class
</span></span> Customer
<span style=
"color: #990000"><</span> ActiveRecord
<span style=
"color: #990000">::
</span>Base
2538 has_many
<span style=
"color: #990000">:
</span>orders
<span style=
"color: #990000">,
</span> <span style=
"color: #990000">:
</span>extend
<span style=
"color: #990000">=
></span> <span style=
"color: #990000">[
</span>FindRecentExtension
<span style=
"color: #990000">,
</span> FindActiveExtension
<span style=
"color: #990000">]
</span>
2539 <span style=
"font-weight: bold"><span style=
"color: #0000FF">end
</span></span>
2540 </tt></pre></div></div>
2541 <div class=
"para"><p>Extensions can refer to the internals of the association proxy using these three accessors:
</p></div>
2542 <div class=
"ilist"><ul>
2545 <tt>proxy_owner
</tt> returns the object that the association is a part of.
2550 <tt>proxy_reflection
</tt> returns the reflection object that describes the association.
2555 <tt>proxy_target
</tt> returns the associated object for
<tt>belongs_to
</tt> or
<tt>has_one
</tt>, or the collection of associated objects for
<tt>has_many
</tt> or
<tt>has_and_belongs_to_many
</tt>.
2560 <h2 id=
"_changelog">5. Changelog
</h2>
2561 <div class=
"sectionbody">
2562 <div class=
"para"><p><a href=
"http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/11">Lighthouse ticket
</a></p></div>
2563 <div class=
"ilist"><ul>
2566 September
28,
2008: Corrected
<tt>has_many :through
</tt> diagram, added polymorphic diagram, some reorganization by
<a href=
"../authors.html#mgunderloy">Mike Gunderloy
</a> . First release version.
2571 September
22,
2008: Added diagrams, misc. cleanup by
<a href=
"../authors.html#mgunderloy">Mike Gunderloy
</a> (not yet approved for publication)
2576 September
14,
2008: initial version by
<a href=
"../authors.html#mgunderloy">Mike Gunderloy
</a> (not yet approved for publication)