guides/source/association_basics.textile in railties-3.0.0.beta2 vs guides/source/association_basics.textile in railties-3.0.0.beta3
- old
+ new
@@ -415,11 +415,11 @@
h5. Creating Join Tables for +has_and_belongs_to_many+ Associations
If you create a +has_and_belongs_to_many+ association, you need to explicitly create the joining table. Unless the name of the join table is explicitly specified by using the +:join_table+ option, Active Record creates 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.
-WARNING: The precedence between model names is calculated using the +<+ operator for +String+. 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" (because the underscore '_' is lexicographically _less_ than 's' in common encodings).
+WARNING: The precedence between model names is calculated using the +<+ operator for +String+. 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" (because the underscore '_' is lexicographically _less_ than 's' in common encodings).
Whatever the name, you must manually generate the join table with an appropriate migration. For example, consider these associations:
<ruby>
class Assembly < ActiveRecord::Base
@@ -601,25 +601,25 @@
* +:readonly+
* +:select+
* +:touch+
* +:validate+
-h6. +:autosave+
+h6(#belongs_to-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.
-h6. +:class_name+
+h6(#belongs_to-class_name). +:class_name+
If the name of the other model cannot be derived from the association name, you can use the +:class_name+ 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 +Patron+, you'd set things up this way:
<ruby>
class Order < ActiveRecord::Base
belongs_to :customer, :class_name => "Patron"
end
</ruby>
-h6. +:conditions+
+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).
<ruby>
class Order < ActiveRecord::Base
@@ -664,17 +664,17 @@
end
</ruby>
Counter cache columns are added to the containing model's list of read-only attributes through +attr_readonly+.
-h6. +:dependent+
+h6(#belongs_to-dependent). +:dependent+
If you set the +:dependent+ option to +:destroy+, then deleting this object will call the +destroy+ method on the associated object to delete that object. If you set the +:dependent+ option to +:delete+, then deleting this object will delete the associated object _without_ calling its +destroy+ method.
WARNING: You should not specify this option on a +belongs_to+ association that is connected with a +has_many+ association on the other class. Doing so can lead to orphaned records in your database.
-h6. +:foreign_key+
+h6(#belongs_to-foreign_key). +:foreign_key+
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 +_id+ added. The +:foreign_key+ option lets you set the name of the foreign key directly:
<ruby>
class Order < ActiveRecord::Base
@@ -683,11 +683,11 @@
end
</ruby>
TIP: In any case, Rails will not create foreign key columns for you. You need to explicitly define them as part of your migrations.
-h6. +:include+
+h6(#belongs_to-includes). +:include+
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:
<ruby>
class LineItem < ActiveRecord::Base
@@ -725,15 +725,15 @@
h6. +: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>.
-h6. +:readonly+
+h6(#belongs_to-readonly). +:readonly+
If you set the +:readonly+ option to +true+, then the associated object will be read-only when retrieved via the association.
-h6. +:select+
+h6(#belongs_to-select). +:select+
The +:select+ option lets you override the SQL +SELECT+ clause that is used to retrieve data about the associated object. By default, Rails retrieves all columns.
TIP: If you set the +:select+ option on a +belongs_to+ association, you should also set the +foreign_key+ option to guarantee the correct results.
@@ -757,25 +757,25 @@
class Order < ActiveRecord::Base
belongs_to :customer, :touch => :orders_updated_at
end
</ruby>
-h6. +:validate+
+h6(#belongs_to-validate). +:validate+
If you set the +:validate+ option to +true+, then associated objects will be validated whenever you save this object. By default, this is +false+: associated objects will not be validated when this object is saved.
-h5. How To Know Whether There's an Associated Object?
+h5(#belongs_to-how_to_know_whether_theres_an_associated_object). How To Know Whether There's an Associated Object?
To know whether there's and associated object just check <tt><em>association</em>.nil?</tt>:
<ruby>
if @order.customer.nil?
@msg = "No customer found for this order"
end
</ruby>
-h5. When are Objects Saved?
+h5(#belongs_to-when_are_objects_saved). When are Objects Saved?
Assigning an object to a +belongs_to+ association does _not_ automatically save the object. It does not save the associated object either.
h4. +has_one+ Association Reference
@@ -867,43 +867,43 @@
* +:source+
* +:source_type+
* +:through+
* +:validate+
-h6. +:as+
+h6(#has_one-as). +:as+
Setting the +:as+ option indicates that this is a polymorphic association. Polymorphic associations were discussed in detail <a href="#polymorphic-associations">earlier in this guide</a>.
-h6. +:autosave+
+h6(#has_one-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.
-h6. +:class_name+
+h6(#has_one-class_name). +:class_name+
If the name of the other model cannot be derived from the association name, you can use the +:class_name+ 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:
<ruby>
class Supplier < ActiveRecord::Base
has_one :account, :class_name => "Billing"
end
</ruby>
-h6. +:conditions+
+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).
<ruby>
class Supplier < ActiveRecord::Base
has_one :account, :conditions => "confirmed = 1"
end
</ruby>
-h6. +:dependent+
+h6(#has_one-dependent). +:dependent+
If you set the +:dependent+ option to +:destroy+, then deleting this object will call the +destroy+ method on the associated object to delete that object. If you set the +:dependent+ option to +:delete+, then deleting this object will delete the associated object _without_ calling its +destroy+ method. If you set the +:dependent+ option to +:nullify+, then deleting this object will set the foreign key in the association object to +NULL+.
-h6. +:foreign_key+
+h6(#has_one-foreign_key). +:foreign_key+
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 +_id+ added. The +:foreign_key+ option lets you set the name of the foreign key directly:
<ruby>
class Supplier < ActiveRecord::Base
@@ -911,11 +911,11 @@
end
</ruby>
TIP: In any case, Rails will not create foreign key columns for you. You need to explicitly define them as part of your migrations.
-h6. +:include+
+h6(#has_one-include). +:include+
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:
<ruby>
class Supplier < ActiveRecord::Base
@@ -947,53 +947,53 @@
class Representative < ActiveRecord::Base
has_many :accounts
end
</ruby>
-h6. +:order+
+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.
-h6. +:primary_key+
+h6(#has_one-primary_key). +:primary_key+
By convention, Rails guesses that the column used to hold the primary key of this model is +id+. You can override this and explicitly specify the primary key with the +:primary_key+ option.
-h6. +:readonly+
+h6(#has_one-readonly). +:readonly+
If you set the +:readonly+ option to +true+, then the associated object will be read-only when retrieved via the association.
-h6. +:select+
+h6(#has_one-select). +:select+
The +:select+ option lets you override the SQL +SELECT+ clause that is used to retrieve data about the associated object. By default, Rails retrieves all columns.
-h6. +:source+
+h6(#has_one-source). +:source+
The +:source+ option specifies the source association name for a +has_one :through+ association.
-h6. +:source_type+
+h6(#has_one-source_type). +:source_type+
The +:source_type+ option specifies the source association type for a +has_one :through+ association that proceeds through a polymorphic association.
-h6. +:through+
+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>.
-h6. +:validate+
+h6(#has_one-validate). +:validate+
If you set the +:validate+ option to +true+, then associated objects will be validated whenever you save this object. By default, this is +false+: associated objects will not be validated when this object is saved.
-h5. How To Know Whether There's an Associated Object?
+h5(#has_one-how_to_know_whether_theres_an_associated_object). How To Know Whether There's an Associated Object?
To know whether there's and associated object just check <tt><em>association</em>.nil?</tt>:
<ruby>
if @supplier.account.nil?
@msg = "No account found for this supplier"
end
</ruby>
-h5. When are Objects Saved?
+h5(#has_one-when_are_objects_saved). When are Objects Saved?
When you assign an object to a +has_one+ 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.
If either of these saves fails due to validation errors, then the assignment statement returns +false+ and the assignment itself is cancelled.
@@ -1003,11 +1003,11 @@
h4. +has_many+ Association Reference
The +has_many+ 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.
-h5. Methods Added
+h5. Methods Added by +has_many+
When you declare a +has_many+ association, the declaring class automatically gains 13 methods related to the association:
* <tt><em>collection</em>(force_reload = false)</tt>
* <tt><em>collection</em><<(object, ...)</tt>
@@ -1047,54 +1047,54 @@
orders.exists?(...)
orders.build(attributes = {}, ...)
orders.create(attributes = {})
</ruby>
-h6. <tt><em>collection</em>(force_reload = false)</tt>
+h6(#has_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.
<ruby>
@orders = @customer.orders
</ruby>
-h6. <tt><em>collection</em><<(object, ...)</tt>
+h6(#has_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 setting their foreign keys to the primary key of the calling model.
<ruby>
@customer.orders << @order1
</ruby>
-h6. <tt><em>collection</em>.delete(object, ...)</tt>
+h6(#has_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 setting their foreign keys to +NULL+.
<ruby>
@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+.
-h6. <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.
-h6. <tt><em>collection_singular</em>_ids</tt>
+h6(#has_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.
<ruby>
@order_ids = @customer.order_ids
</ruby>
-h6. <tt><em>collection_singular</em>_ids=ids</tt>
+h6(#has_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_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>
@@ -1177,29 +1177,29 @@
* +:source_type+
* +:through+
* +:uniq+
* +:validate+
-h6. +:as+
+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+
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.
-h6. +:class_name+
+h6(#has_many-class_name). +:class_name+
If the name of the other model cannot be derived from the association name, you can use the +:class_name+ option to supply the model name. For example, if a customer has many orders, but the actual name of the model containing orders is +Transaction+, you'd set things up this way:
<ruby>
class Customer < ActiveRecord::Base
has_many :orders, :class_name => "Transaction"
end
</ruby>
-h6. +:conditions+
+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).
<ruby>
class Customer < ActiveRecord::Base
@@ -1228,31 +1228,31 @@
end
</ruby>
Be sure to use single quotes.
-h6. +:counter_sql+
+h6(#has_many-counter_sql). +:counter_sql+
Normally Rails automatically generates the proper SQL to count the association members. With the +:counter_sql+ option, you can specify a complete SQL statement to count them yourself.
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. +:dependent+
+h6(#has_many-dependent). +:dependent+
If you set the +:dependent+ option to +:destroy+, then deleting this object will call the +destroy+ method on the associated objects to delete those objects. If you set the +:dependent+ option to +:delete_all+, then deleting this object will delete the associated objects _without_ calling their +destroy+ method. If you set the +:dependent+ option to +:nullify+, then deleting this object will set the foreign key in the associated objects to +NULL+.
NOTE: This option is ignored when you use the +:through+ option on the association.
-h6. +:extend+
+h6(#has_many-extend). +:extend+
The +:extend+ option specifies a named module to extend the association proxy. Association extensions are discussed in detail <a href="#association-extensions">later in this guide</a>.
-h6. +:finder_sql+
+h6(#has_many-finder_sql). +:finder_sql+
Normally Rails automatically generates the proper SQL to fetch the association members. With the +:finder_sql+ option, you can specify a complete SQL statement to fetch them yourself. If fetching objects requires complex multi-table SQL, this may be necessary.
-h6. +:foreign_key+
+h6(#has_many-foreign_key). +:foreign_key+
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 +_id+ added. The +:foreign_key+ option lets you set the name of the foreign key directly:
<ruby>
class Customer < ActiveRecord::Base
@@ -1260,21 +1260,21 @@
end
</ruby>
TIP: In any case, Rails will not create foreign key columns for you. You need to explicitly define them as part of your migrations.
-h6. +:group+
+h6(:has_many-group). +:group+
The +:group+ option supplies an attribute name to group the result set by, using a +GROUP BY+ clause in the finder SQL.
<ruby>
class Customer < ActiveRecord::Base
has_many :line_items, :through => :orders, :group => "orders.id"
end
</ruby>
-h6. +:include+
+h6(#has_many-include). +:include+
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:
<ruby>
class Customer < ActiveRecord::Base
@@ -1306,70 +1306,70 @@
class LineItem < ActiveRecord::Base
belongs_to :order
end
</ruby>
-h6. +:limit+
+h6(#has_many-limit). +:limit+
The +:limit+ option lets you restrict the total number of objects that will be fetched through an association.
<ruby>
class Customer < ActiveRecord::Base
has_many :recent_orders, :class_name => "Order",
:order => "order_date DESC", :limit => 100
end
</ruby>
-h6. +:offset+
+h6(#has_many-offset). +:offset+
The +:offset+ option lets you specify the starting offset for fetching objects via an association. For example, if you set +:offset => 11+, it will skip the first 11 records.
-h6. +:order+
+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).
<ruby>
class Customer < ActiveRecord::Base
has_many :orders, :order => "date_confirmed DESC"
end
</ruby>
-h6. +:primary_key+
+h6(#has_many-primary_key). +:primary_key+
By convention, Rails guesses that the column used to hold the primary key of the association is +id+. You can override this and explicitly specify the primary key with the +:primary_key+ option.
-h6. +:readonly+
+h6(#has_many-readonly). +:readonly+
If you set the +:readonly+ option to +true+, then the associated objects will be read-only when retrieved via the association.
-h6. +:select+
+h6(#has_many-select). +:select+
The +:select+ option lets you override the SQL +SELECT+ clause that is used to retrieve data about the associated objects. By default, Rails retrieves all columns.
WARNING: If you specify your own +:select+, be sure to include the primary key and foreign key columns of the associated model. If you do not, Rails will throw an error.
-h6. +:source+
+h6(#has_many-source). +:source+
The +:source+ option specifies the source association name for a +has_many :through+ association. You only need to use this option if the name of the source association cannot be automatically inferred from the association name.
-h6. +:source_type+
+h6(#has_many-source_type). +:source_type+
The +:source_type+ option specifies the source association type for a +has_many :through+ association that proceeds through a polymorphic association.
-h6. +:through+
+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>.
-h6. +:uniq+
+h6(#has_many-uniq). +:uniq+
Specify the +:uniq => true+ option to remove duplicates from the collection. This is most useful in conjunction with the +:through+ option.
-h6. +:validate+
+h6(#has_many-validate). +:validate+
If you set the +:validate+ option to +false+, then associated objects will not be validated whenever you save this object. By default, this is +true+: associated objects will be validated when this object is saved.
-h5. When are Objects Saved?
+h5(#has_many-when_are_objects_saved). When are Objects Saved?
When you assign an object to a +has_many+ 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.
If any of these saves fails due to validation errors, then the assignment statement returns +false+ and the assignment itself is cancelled.
@@ -1379,11 +1379,11 @@
h4. +has_and_belongs_to_many+ Association Reference
The +has_and_belongs_to_many+ 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.
-h5. Methods Added
+h5. Methods Added by +has_and_belongs_to_many+
When you declare a +has_and_belongs_to_many+ association, the declaring class automatically gains 13 methods related to the association:
* <tt><em>collection</em>(force_reload = false)</tt>
* <tt><em>collection</em><<(object, ...)</tt>
@@ -1476,38 +1476,38 @@
h6. <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.
-h6. <tt><em>collection</em>.empty?</tt>
+h6(#has_and_belongs_to_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.
<ruby>
<% if @part.assemblies.empty? %>
This part is not used in any assemblies
<% end %>
</ruby>
-h6. <tt><em>collection</em>.size</tt>
+h6(#has_and_belongs_to_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.
<ruby>
@assembly_count = @part.assemblies.size
</ruby>
-h6. <tt><em>collection</em>.find(...)</tt>
+h6(#has_and_belongs_to_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+. It also adds the additional condition that the object must be in the collection.
<ruby>
@new_assemblies = @part.assemblies.find(:all,
:conditions => ["created_at > ?", 2.days.ago])
</ruby>
-h6. <tt><em>collection</em>.exists?(...)</tt>
+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>
@@ -1516,11 +1516,11 @@
<ruby>
@assembly = @part.assemblies.build(
{:assembly_name => "Transmission housing"})
</ruby>
-h6. <tt><em>collection</em>.create(attributes = {})</tt>
+h6(#has_and_belongs_to_many-create-attributes). <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 the join table will be created, and the associated object _will_ be saved (assuming that it passes any validations).
<ruby>
@assembly = @part.assemblies.create(
@@ -1573,25 +1573,25 @@
:foreign_key => "this_user_id",
:association_foreign_key => "other_user_id"
end
</ruby>
-h6. +:autosave+
+h6(#has_and_belongs_to_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.
-h6. +:class_name+
+h6(#has_and_belongs_to_many-class_name). +:class_name+
If the name of the other model cannot be derived from the association name, you can use the +:class_name+ option to supply the model name. For example, if a part has many assemblies, but the actual name of the model containing assemblies is +Gadget+, you'd set things up this way:
<ruby>
class Parts < ActiveRecord::Base
has_and_belongs_to_many :assemblies, :class_name => "Gadget"
end
</ruby>
-h6. +:conditions+
+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).
<ruby>
class Parts < ActiveRecord::Base
@@ -1609,29 +1609,29 @@
end
</ruby>
If you use a hash-style +:conditions+ option, then record creation via this association will be automatically scoped using the hash. In this case, using +@parts.assemblies.create+ or +@parts.assemblies.build+ will create orders where the +factory+ column has the value "Seattle".
-h6. +:counter_sql+
+h6(#has_and_belongs_to_many-counter_sql). +:counter_sql+
Normally Rails automatically generates the proper SQL to count the association members. With the +:counter_sql+ option, you can specify a complete SQL statement to count them yourself.
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+
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.
-h6. +:extend+
+h6(#has_and_belongs_to_many-extend). +:extend+
The +:extend+ option specifies a named module to extend the association proxy. Association extensions are discussed in detail <a href="#association-extensions">later in this guide</a>.
-h6. +:finder_sql+
+h6(#has_and_belongs_to_many-finder_sql). +:finder_sql+
Normally Rails automatically generates the proper SQL to fetch the association members. With the +:finder_sql+ option, you can specify a complete SQL statement to fetch them yourself. If fetching objects requires complex multi-table SQL, this may be necessary.
-h6. +:foreign_key+
+h6(#has_and_belongs_to_many-foreign_key). +:foreign_key+
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 +_id+ added. The +:foreign_key+ option lets you set the name of the foreign key directly:
<ruby>
class User < ActiveRecord::Base
@@ -1639,21 +1639,21 @@
:foreign_key => "this_user_id",
:association_foreign_key => "other_user_id"
end
</ruby>
-h6. +:group+
+h6(#has_and_belongs_to_many-group). +:group+
The +:group+ option supplies an attribute name to group the result set by, using a +GROUP BY+ clause in the finder SQL.
<ruby>
class Parts < ActiveRecord::Base
has_and_belongs_to_many :assemblies, :group => "factory"
end
</ruby>
-h6. +:include+
+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+
@@ -1661,52 +1661,52 @@
h6. +: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.
-h6. +:limit+
+h6(#has_and_belongs_to_many-limit). +:limit+
The +:limit+ option lets you restrict the total number of objects that will be fetched through an association.
<ruby>
class Parts < ActiveRecord::Base
has_and_belongs_to_many :assemblies, :order => "created_at DESC",
:limit => 50
end
</ruby>
-h6. +:offset+
+h6(#has_and_belongs_to_many-offset). +:offset+
The +:offset+ option lets you specify the starting offset for fetching objects via an association. For example, if you set +:offset => 11+, it will skip the first 11 records.
-h6. +:order+
+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).
<ruby>
class Parts < ActiveRecord::Base
has_and_belongs_to_many :assemblies, :order => "assembly_name ASC"
end
</ruby>
-h6. +:readonly+
+h6(#has_and_belongs_to_many-readonly). +:readonly+
If you set the +:readonly+ option to +true+, then the associated objects will be read-only when retrieved via the association.
-h6. +:select+
+h6(#has_and_belongs_to_many-select). +:select+
The +:select+ option lets you override the SQL +SELECT+ clause that is used to retrieve data about the associated objects. By default, Rails retrieves all columns.
-h6. +:uniq+
+h6(#has_and_belongs_to_many-uniq). +:uniq+
Specify the +:uniq => true+ option to remove duplicates from the collection.
-h6. +:validate+
+h6(#has_and_belongs_to_many-validate). +:validate+
If you set the +:validate+ option to +false+, then associated objects will not be validated whenever you save this object. By default, this is +true+: associated objects will be validated when this object is saved.
-h5. When are Objects Saved?
+h5(#has_and_belongs_to_many-when_are_objects_saved). When are Objects Saved?
When you assign an object to a +has_and_belongs_to_many+ 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.
If any of these saves fails due to validation errors, then the assignment statement returns +false+ and the assignment itself is cancelled.
@@ -1807,9 +1807,10 @@
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
* September 28, 2008: Corrected +has_many :through+ diagram, added polymorphic diagram, some reorganization by "Mike Gunderloy":credits.html#mgunderloy . First release version.
* September 22, 2008: Added diagrams, misc. cleanup by "Mike Gunderloy":credits.html#mgunderloy (not yet approved for publication)
* September 14, 2008: initial version by "Mike Gunderloy":credits.html#mgunderloy (not yet approved for publication)