aboutsummaryrefslogtreecommitdiffstats
path: root/railties/guides/source/association_basics.textile
diff options
context:
space:
mode:
Diffstat (limited to 'railties/guides/source/association_basics.textile')
-rw-r--r--railties/guides/source/association_basics.textile141
1 files changed, 87 insertions, 54 deletions
diff --git a/railties/guides/source/association_basics.textile b/railties/guides/source/association_basics.textile
index b1ee4b8be4..e5b8c73c43 100644
--- a/railties/guides/source/association_basics.textile
+++ b/railties/guides/source/association_basics.textile
@@ -30,7 +30,7 @@ Now, suppose we wanted to add a new order for an existing customer. We'd need to
Or consider deleting a customer, and ensuring that all of its orders get deleted as well:
<ruby>
-@orders = Order.find_all_by_customer_id(@customer.id)
+@orders = Order.where(:customer_id => @customer.id)
@orders.each do |order|
order.destroy
end
@@ -65,7 +65,7 @@ To learn more about the different types of associations, read the next section o
h3. The Types of Associations
-In Rails, an _association_ 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 +belongs_to+ 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:
+In Rails, an _association_ 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 +belongs_to+ 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 associations:
* +belongs_to+
* +has_one+
@@ -165,6 +165,12 @@ class Paragraph < ActiveRecord::Base
end
</ruby>
+With +:through => :sections+ specified, Rails will now understand:
+
+<ruby>
+@document.paragraphs
+</ruby>
+
h4. The +has_one :through+ Association
A +has_one :through+ 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 _through_ 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:
@@ -550,7 +556,9 @@ build_customer
create_customer
</ruby>
-h6. _association_(force_reload = false)
+NOTE: When initializing a new +has_one+ or +belongs_to+ association you must use the +build_+ prefix to build the association, rather than the +association.build+ method that would be used for +has_many+ or +has_and_belongs_to_many+ associations. To create one, use the +create_+ prefix.
+
+h6(#belongs_to-association). <tt><em>association</em>(force_reload = false)</tt>
The <tt><em>association</em></tt> method returns the associated object, if any. If no associated object is found, it returns +nil+.
@@ -560,7 +568,7 @@ The <tt><em>association</em></tt> method returns the associated object, if any.
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 +true+ as the +force_reload+ argument.
-h6. _association_=(associate)
+h6(#belongs_to-association_equal). <tt>_association_=(associate)</tt>
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.
@@ -568,7 +576,7 @@ The <tt><em>association</em>=</tt> method assigns an associated object to this o
@order.customer = @customer
</ruby>
-h6. build_<em>association</em>(attributes = {})
+h6(#belongs_to-build_association). <tt>build_<em>association</em>(attributes = {})</tt>
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.
@@ -577,7 +585,7 @@ The <tt>build_<em>association</em></tt> method returns a new object of the assoc
:customer_name => "John Doe")
</ruby>
-h6. create_<em>association</em>(attributes = {})
+h6(#belongs_to-create_association). <tt>create_<em>association</em>(attributes = {})</tt>
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).
@@ -629,7 +637,7 @@ end
h6(#belongs_to-conditions). +:conditions+
-The +:conditions+ option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL +WHERE+ clause).
+The +:conditions+ option lets you specify the conditions that the associated object must meet (in the syntax used by an SQL +WHERE+ clause).
<ruby>
class Order < ActiveRecord::Base
@@ -637,7 +645,7 @@ class Order < ActiveRecord::Base
end
</ruby>
-h6. +:counter_cache+
+h6(#belongs_to-counter_cache). +:counter_cache+
The +:counter_cache+ option can be used to make finding the number of belonging objects more efficient. Consider these models:
@@ -733,7 +741,7 @@ end
NOTE: There's no need to use +:include+ for immediate associations - that is, if you have +Order belongs_to :customer+, then the customer is eager-loaded automatically when it's needed.
-h6. +:polymorphic+
+h6(#belongs_to-polymorphic). +:polymorphic+
Passing +true+ to the +:polymorphic+ option indicates that this is a polymorphic association. Polymorphic associations were discussed in detail <a href="#polymorphic-associations">earlier in this guide</a>.
@@ -747,7 +755,7 @@ The +:select+ option lets you override the SQL +SELECT+ clause that is used to r
TIP: If you set the +:select+ option on a +belongs_to+ association, you should also set the +foreign_key+ option to guarantee the correct results.
-h6. +:touch+
+h6(#belongs_to-touch). +:touch+
If you set the +:touch+ option to +:true+, then the +updated_at+ or +updated_on+ timestamp on the associated object will be set to the current time whenever this object is saved or destroyed:
@@ -817,7 +825,9 @@ build_account
create_account
</ruby>
-h6. <tt><em>association</em>(force_reload = false)</tt>
+NOTE: When initializing a new +has_one+ or +belongs_to+ association you must use the +build_+ prefix to build the association, rather than the +association.build+ method that would be used for +has_many+ or +has_and_belongs_to_many+ associations. To create one, use the +create_+ prefix.
+
+h6(#has_one-association). <tt><em>association</em>(force_reload = false)</tt>
The <tt><em>association</em></tt> method returns the associated object, if any. If no associated object is found, it returns +nil+.
@@ -827,7 +837,7 @@ The <tt><em>association</em></tt> method returns the associated object, if any.
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 +true+ as the +force_reload+ argument.
-h6. <tt><em>association</em>=(associate)</tt>
+h6(#has_one-association_equal). <tt><em>association</em>=(associate)</tt>
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.
@@ -835,7 +845,7 @@ The <tt><em>association</em>=</tt> method assigns an associated object to this o
@supplier.account = @account
</ruby>
-h6. <tt>build_<em>association</em>(attributes = {})</tt>
+h6(#has_one-build_association). <tt>build_<em>association</em>(attributes = {})</tt>
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.
@@ -843,7 +853,7 @@ The <tt>build_<em>association</em></tt> method returns a new object of the assoc
@account = @supplier.build_account(:terms => "Net 30")
</ruby>
-h6. <tt>create_<em>association</em>(attributes = {})</tt>
+h6(#has_one-create_association). <tt>create_<em>association</em>(attributes = {})</tt>
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).
@@ -899,7 +909,7 @@ end
h6(#has_one-conditions). +:conditions+
-The +:conditions+ option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL +WHERE+ clause).
+The +:conditions+ option lets you specify the conditions that the associated object must meet (in the syntax used by an SQL +WHERE+ clause).
<ruby>
class Supplier < ActiveRecord::Base
@@ -961,7 +971,7 @@ end
h6(#has_one-order). +:order+
-The +:order+ option dictates the order in which associated objects will be received (in the syntax used by a SQL +ORDER BY+ clause). Because a +has_one+ association will only retrieve a single associated object, this option should not be needed.
+The +:order+ option dictates the order in which associated objects will be received (in the syntax used by an SQL +ORDER BY+ clause). Because a +has_one+ association will only retrieve a single associated object, this option should not be needed.
h6(#has_one-primary_key). +:primary_key+
@@ -985,7 +995,7 @@ The +:source_type+ option specifies the source association type for a +has_one :
h6(#has_one-through). +:through+
-The +:through+ option specifies a join model through which to perform the query. +has_one :through+ associations were discussed in detail <a href="#the-has-one-through-association">earlier in this guide</a>.
+The +:through+ option specifies a join model through which to perform the query. +has_one :through+ associations were discussed in detail <a href="#the-has_one-through-association">earlier in this guide</a>.
h6(#has_one-validate). +:validate+
@@ -1029,6 +1039,7 @@ When you declare a +has_many+ association, the declaring class automatically gai
* <tt><em>collection</em>.empty?</tt>
* <tt><em>collection</em>.size</tt>
* <tt><em>collection</em>.find(...)</tt>
+* <tt><em>collection</em>.where(...)</tt>
* <tt><em>collection</em>.exists?(...)</tt>
* <tt><em>collection</em>.build(attributes = {}, ...)</tt>
* <tt><em>collection</em>.create(attributes = {})</tt>
@@ -1054,6 +1065,7 @@ orders.clear
orders.empty?
orders.size
orders.find(...)
+orders.where(...)
orders.exists?(...)
orders.build(attributes = {}, ...)
orders.create(attributes = {})
@@ -1083,10 +1095,10 @@ The <tt><em>collection</em>.delete</tt> method removes one or more objects from
@customer.orders.delete(@order1)
</ruby>
-WARNING: Objects will be in addition destroyed if they're associated with +:dependent => :destroy+, and deleted if they're associated with +:dependent => :delete_all+.
+WARNING: Additionally, objects will be destroyed if they're associated with +:dependent => :destroy+, and deleted if they're associated with +:dependent => :delete_all+.
-h6(#has_many-collection_equal). <tt><em>collection</em>=objects</tt>
+h6(#has_many-collection-equal). <tt><em>collection</em>=objects</tt>
The <tt><em>collection</em>=</tt> method makes the collection contain only the supplied objects, by adding and deleting as appropriate.
@@ -1102,11 +1114,11 @@ h6(#has_many-collection_singular_ids_ids). <tt><em>collection_singular</em>_ids=
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.
-h6(#has_many-collection_clear). <tt><em>collection</em>.clear</tt>
+h6(#has_many-collection-clear). <tt><em>collection</em>.clear</tt>
The <tt><em>collection</em>.clear</tt> method removes every object from the collection. This destroys the associated objects if they are associated with +:dependent => :destroy+, deletes them directly from the database if +:dependent => :delete_all+, and otherwise sets their foreign keys to +NULL+.
-h6. <tt><em>collection</em>.empty?</tt>
+h6(#has_many-collection-empty). <tt><em>collection</em>.empty?</tt>
The <tt><em>collection</em>.empty?</tt> method returns +true+ if the collection does not contain any associated objects.
@@ -1116,7 +1128,7 @@ The <tt><em>collection</em>.empty?</tt> method returns +true+ if the collection
<% end %>
</ruby>
-h6. <tt><em>collection</em>.size</tt>
+h6(#has_many-collection-size). <tt><em>collection</em>.size</tt>
The <tt><em>collection</em>.size</tt> method returns the number of objects in the collection.
@@ -1124,19 +1136,30 @@ The <tt><em>collection</em>.size</tt> method returns the number of objects in th
@order_count = @customer.orders.size
</ruby>
-h6. <tt><em>collection</em>.find(...)</tt>
+h6(#has_many-collection-find). <tt><em>collection</em>.find(...)</tt>
The <tt><em>collection</em>.find</tt> method finds objects within the collection. It uses the same syntax and options as +ActiveRecord::Base.find+.
<ruby>
-@open_orders = @customer.orders.find(:all, :conditions => "open = 1")
+@open_orders = @customer.orders.all(:conditions => "open = 1")
</ruby>
-h6. <tt><em>collection</em>.exists?(...)</tt>
+NOTE: Starting Rails 3, supplying options to +ActiveRecord::Base.find+ method is discouraged. Use <tt><em>collection</em>.where</tt> instead when you need to pass conditions.
+
+h6(#has_many-collection-where). <tt><em>collection</em>.where(...)</tt>
+
+The <tt><em>collection</em>.where</tt> method finds objects within the collection based on the conditions supplied but the objects are loaded lazily meaning that the database is queried only when the object(s) are accessed.
+
+<ruby>
+@open_orders = @customer.orders.where(:open => true) # No query yet
+@open_order = @open_orders.first # Now the database will be queried
+</ruby>
+
+h6(#has_many-collection-exists). <tt><em>collection</em>.exists?(...)</tt>
The <tt><em>collection</em>.exists?</tt> method checks whether an object meeting the supplied conditions exists in the collection. It uses the same syntax and options as +ActiveRecord::Base.exists?+.
-h6. <tt><em>collection</em>.build(attributes = {}, ...)</tt>
+h6(#has_many-collection-build). <tt><em>collection</em>.build(attributes = {}, ...)</tt>
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 _not_ yet be saved.
@@ -1145,7 +1168,7 @@ The <tt><em>collection</em>.build</tt> method returns one or more new objects of
:order_number => "A12345")
</ruby>
-h6. <tt><em>collection</em>.create(attributes = {})</tt>
+h6(#has_many-collection-create). <tt><em>collection</em>.create(attributes = {})</tt>
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 _will_ be saved (assuming that it passes any validations).
@@ -1193,7 +1216,7 @@ h6(#has_many-as). +:as+
Setting the +:as+ option indicates that this is a polymorphic association, as discussed <a href="#polymorphic-associations">earlier in this guide</a>.
-h6. +:autosave+
+h6(#has_many-autosave). +:autosave+
If you set the +:autosave+ option to +true+, Rails will save any loaded members and destroy members that are marked for destruction whenever you save the parent object.
@@ -1209,7 +1232,7 @@ end
h6(#has_many-conditions). +:conditions+
-The +:conditions+ option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL +WHERE+ clause).
+The +:conditions+ option lets you specify the conditions that the associated object must meet (in the syntax used by an SQL +WHERE+ clause).
<ruby>
class Customer < ActiveRecord::Base
@@ -1335,7 +1358,7 @@ The +:offset+ option lets you specify the starting offset for fetching objects v
h6(#has_many-order). +:order+
-The +:order+ option dictates the order in which associated objects will be received (in the syntax used by a SQL +ORDER BY+ clause).
+The +:order+ option dictates the order in which associated objects will be received (in the syntax used by an SQL +ORDER BY+ clause).
<ruby>
class Customer < ActiveRecord::Base
@@ -1367,7 +1390,7 @@ The +:source_type+ option specifies the source association type for a +has_many
h6(#has_many-through). +:through+
-The +:through+ option specifies a join model through which to perform the query. +has_many :through+ associations provide a way to implement many-to-many relationships, as discussed <a href="#the-has-many-through-association">earlier in this guide</a>.
+The +:through+ option specifies a join model through which to perform the query. +has_many :through+ associations provide a way to implement many-to-many relationships, as discussed <a href="#the-has_many-through-association">earlier in this guide</a>.
h6(#has_many-uniq). +:uniq+
@@ -1401,8 +1424,8 @@ person = Person.create(:name => 'honda')
post = Post.create(:name => 'a1')
person.posts << post
person.posts << post
-person.posts.inspect # => [#<Post id: 7, name: "a1">]
-Reading.all.inspect # => [#<Reading id: 16, person_id: 7, post_id: 7>, #<Reading id: 17, person_id: 7, post_id: 7>]
+person.posts.inspect # => [#<Post id: 7, name: "a1">]
+Reading.all.inspect # => [#<Reading id: 16, person_id: 7, post_id: 7>, #<Reading id: 17, person_id: 7, post_id: 7>]
</ruby>
In the above case there are still two readings. However +person.posts+ shows only one post because the collection loads only unique records.
@@ -1439,11 +1462,12 @@ When you declare a +has_and_belongs_to_many+ association, the declaring class au
* <tt><em>collection</em>.empty?</tt>
* <tt><em>collection</em>.size</tt>
* <tt><em>collection</em>.find(...)</tt>
+* <tt><em>collection</em>.where(...)</tt>
* <tt><em>collection</em>.exists?(...)</tt>
* <tt><em>collection</em>.build(attributes = {})</tt>
* <tt><em>collection</em>.create(attributes = {})</tt>
-In all of these methods, <tt><em>collection</em></tt> is replaced with the symbol passed as the first argument to +has_and_belongs_to_many+, and <tt><em>collection_singular</em></tt> is replaced with the singularized version of that symbol.. For example, given the declaration:
+In all of these methods, <tt><em>collection</em></tt> is replaced with the symbol passed as the first argument to +has_and_belongs_to_many+, and <tt><em>collection_singular</em></tt> is replaced with the singularized version of that symbol. For example, given the declaration:
<ruby>
class Part < ActiveRecord::Base
@@ -1464,6 +1488,7 @@ assemblies.clear
assemblies.empty?
assemblies.size
assemblies.find(...)
+assemblies.where(...)
assemblies.exists?(...)
assemblies.build(attributes = {}, ...)
assemblies.create(attributes = {})
@@ -1476,7 +1501,7 @@ If the join table for a +has_and_belongs_to_many+ association has additional col
WARNING: The use of extra attributes on the join table in a +has_and_belongs_to_many+ 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 +has_many :through+ association instead of +has_and_belongs_to_many+.
-h6. <tt><em>collection</em>(force_reload = false)</tt>
+h6(#has_and_belongs_to_many-collection). <tt><em>collection</em>(force_reload = false)</tt>
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.
@@ -1484,7 +1509,7 @@ The <tt><em>collection</em></tt> method returns an array of all of the associate
@assemblies = @part.assemblies
</ruby>
-h6. <tt><em>collection</em><<(object, ...)</tt>
+h6(#has_and_belongs_to_many-collection-lt_lt). <tt><em>collection</em><<(object, ...)</tt>
The <tt><em>collection</em><<</tt> method adds one or more objects to the collection by creating records in the join table.
@@ -1494,7 +1519,7 @@ The <tt><em>collection</em><<</tt> method adds one or more objects to the collec
NOTE: This method is aliased as <tt><em>collection</em>.concat</tt> and <tt><em>collection</em>.push</tt>.
-h6. <tt><em>collection</em>.delete(object, ...)</tt>
+h6(#has_and_belongs_to_many-collection-delete). <tt><em>collection</em>.delete(object, ...)</tt>
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.
@@ -1502,11 +1527,11 @@ The <tt><em>collection</em>.delete</tt> method removes one or more objects from
@part.assemblies.delete(@assembly1)
</ruby>
-h6. <tt><em>collection</em>=objects</tt>
+h6(#has_and_belongs_to_many-collection-equal). <tt><em>collection</em>=objects</tt>
The <tt><em>collection</em>=</tt> method makes the collection contain only the supplied objects, by adding and deleting as appropriate.
-h6. <tt><em>collection_singular</em>_ids</tt>
+h6(#has_and_belongs_to_many-collection_singular). <tt><em>collection_singular</em>_ids</tt>
The <tt><em>collection_singular</em>_ids</tt> method returns an array of the ids of the objects in the collection.
@@ -1514,11 +1539,11 @@ The <tt><em>collection_singular</em>_ids</tt> method returns an array of the ids
@assembly_ids = @part.assembly_ids
</ruby>
-h6. <tt><em>collection_singular</em>_ids=ids</tt>
+h6(#has_and_belongs_to_many-collection_singular_ids_ids). <tt><em>collection_singular</em>_ids=ids</tt>
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.
-h6. <tt><em>collection</em>.clear</tt>
+h6(#has_and_belongs_to_many-collection-clear). <tt><em>collection</em>.clear</tt>
The <tt><em>collection</em>.clear</tt> method removes every object from the collection by deleting the rows from the joining table. This does not destroy the associated objects.
@@ -1545,15 +1570,25 @@ h6(#has_and_belongs_to_many-collection-find). <tt><em>collection</em>.find(...)<
The <tt><em>collection</em>.find</tt> method finds objects within the collection. It uses the same syntax and options as +ActiveRecord::Base.find+. It also adds the additional condition that the object must be in the collection.
<ruby>
-@new_assemblies = @part.assemblies.find(:all,
+@new_assemblies = @part.assemblies.all(
:conditions => ["created_at > ?", 2.days.ago])
</ruby>
+NOTE: Starting Rails 3, supplying options to +ActiveRecord::Base.find+ method is discouraged. Use <tt><em>collection</em>.where</tt> instead when you need to pass conditions.
+
+h6(#has_and_belongs_to_many-collection-where). <tt><em>collection</em>.where(...)</tt>
+
+The <tt><em>collection</em>.where</tt> method finds objects within the collection based on the conditions supplied but the objects are loaded lazily meaning that the database is queried only when the object(s) are accessed. It also adds the additional condition that the object must be in the collection.
+
+<ruby>
+@new_assemblies = @part.assemblies.where("created_at > ?", 2.days.ago)
+</ruby>
+
h6(#has_and_belongs_to_many-collection-exists). <tt><em>collection</em>.exists?(...)</tt>
The <tt><em>collection</em>.exists?</tt> method checks whether an object meeting the supplied conditions exists in the collection. It uses the same syntax and options as +ActiveRecord::Base.exists?+.
-h6. <tt><em>collection</em>.build(attributes = {})</tt>
+h6(#has_and_belongs_to_many-collection-build). <tt><em>collection</em>.build(attributes = {})</tt>
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 _not_ yet be saved.
@@ -1605,7 +1640,7 @@ The +has_and_belongs_to_many+ association supports these options:
* +:uniq+
* +:validate+
-h6. +:association_foreign_key+
+h6(#has_and_belongs_to_many-association_foreign_key). +:association_foreign_key+
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 +_id+ added. The +:association_foreign_key+ option lets you set the name of the foreign key directly:
@@ -1635,7 +1670,7 @@ end
h6(#has_and_belongs_to_many-conditions). +:conditions+
-The +:conditions+ option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL +WHERE+ clause).
+The +:conditions+ option lets you specify the conditions that the associated object must meet (in the syntax used by an SQL +WHERE+ clause).
<ruby>
class Parts < ActiveRecord::Base
@@ -1661,7 +1696,7 @@ Normally Rails automatically generates the proper SQL to count the association m
NOTE: If you specify +:finder_sql+ but not +:counter_sql+, then the counter SQL will be generated by substituting +SELECT COUNT(*) FROM+ for the +SELECT ... FROM+ clause of your +:finder_sql+ statement.
-h6. +:delete_sql+
+h6(#has_and_belongs_to_many-delete_sql). +:delete_sql+
Normally Rails automatically generates the proper SQL to remove links between the associated classes. With the +:delete_sql+ option, you can specify a complete SQL statement to delete them yourself.
@@ -1699,11 +1734,11 @@ h6(#has_and_belongs_to_many-include). +:include+
You can use the +:include+ option to specify second-order associations that should be eager-loaded when this association is used.
-h6. +:insert_sql+
+h6(#has_and_belongs_to_many-insert_sql). +:insert_sql+
Normally Rails automatically generates the proper SQL to create links between the associated classes. With the +:insert_sql+ option, you can specify a complete SQL statement to insert them yourself.
-h6. +:join_table+
+h6(#has_and_belongs_to_many-join_table). +:join_table+
If the default name of the join table, based on lexical ordering, is not what you want, you can use the +:join_table+ option to override the default.
@@ -1724,7 +1759,7 @@ The +:offset+ option lets you specify the starting offset for fetching objects v
h6(#has_and_belongs_to_many-order). +:order+
-The +:order+ option dictates the order in which associated objects will be received (in the syntax used by a SQL +ORDER BY+ clause).
+The +:order+ option dictates the order in which associated objects will be received (in the syntax used by an SQL +ORDER BY+ clause).
<ruby>
class Parts < ActiveRecord::Base
@@ -1760,9 +1795,9 @@ If you want to assign an object to a +has_and_belongs_to_many+ association witho
h4. Association Callbacks
-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 +:before_save+ callback to cause something to happen just before an object is saved.
+Normal callbacks hook into the life cycle of Active Record objects, allowing you to work with those objects at various points. For example, you can use a +:before_save+ callback to cause something to happen just before an object is saved.
-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:
+Association callbacks are similar to normal callbacks, but they are triggered by events in the life cycle of a collection. There are four available association callbacks:
* +before_add+
* +after_add+
@@ -1821,7 +1856,7 @@ If you have an extension that should be shared by many associations, you can use
<ruby>
module FindRecentExtension
def find_recent
- find(:all, :conditions => ["created_at > ?", 5.days.ago])
+ where("created_at > ?", 5.days.ago)
end
end
@@ -1851,8 +1886,6 @@ Extensions can refer to the internals of the association proxy using these three
h3. Changelog
-"Lighthouse ticket":http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/11
-
* April 7, 2010: Fixed document to validate XHTML 1.0 Strict. "Jaime Iniesta":http://jaimeiniesta.com
* April 19, 2009: Added +:touch+ option to +belongs_to+ associations by "Mike Gunderloy":credits.html#mgunderloy
* February 1, 2009: Added +:autosave+ option "Mike Gunderloy":credits.html#mgunderloy