From 74569f3e204b92e7c96a920b9bfde785323c61f4 Mon Sep 17 00:00:00 2001 From: Mike Gunderloy Date: Mon, 17 Nov 2008 08:37:59 -0600 Subject: Structure change to AR Associations Guide --- railties/doc/guides/html/association_basics.html | 7 +++++-- railties/doc/guides/source/association_basics.txt | 10 ++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) (limited to 'railties') diff --git a/railties/doc/guides/html/association_basics.html b/railties/doc/guides/html/association_basics.html index 9159eaab2a..e8cad6c220 100644 --- a/railties/doc/guides/html/association_basics.html +++ b/railties/doc/guides/html/association_basics.html @@ -742,7 +742,9 @@ customer.orders

3.2. Avoiding Name Collisions

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 ActiveRecord::Base. The association method would override the base method and break things. For instance, attributes or connection are bad names for associations.

3.3. Updating the Schema

-

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:

+

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, depending on what sort of associations you are creating. For belongs_to associations you need to create foreign keys, and for has_and_belongs_to_many associations you need to create the appropriate join table.

+

3.3.1. Creating Foreign Keys for belongs_to Associations

+

When you declare a belongs_to association, you need to create foreign keys as appropriate. For example, consider this model:

end

If you create an association some time after you build the underlying model, you need to remember to create an add_column migration to provide the necessary foreign key.

-

Second, 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 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.

+

3.3.2. 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 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.

diff --git a/railties/doc/guides/source/association_basics.txt b/railties/doc/guides/source/association_basics.txt index 5ba616642b..e0c9ee35d3 100644 --- a/railties/doc/guides/source/association_basics.txt +++ b/railties/doc/guides/source/association_basics.txt @@ -396,7 +396,11 @@ You are not free to use just any name for your associations. Because creating an === Updating the Schema -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: +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, depending on what sort of associations you are creating. For +belongs_to+ associations you need to create foreign keys, and for +has_and_belongs_to_many+ associations you need to create the appropriate join table. + +==== Creating Foreign Keys for +belongs_to+ Associations + +When you declare a +belongs_to+ association, you need to create foreign keys as appropriate. For example, consider this model: [source, ruby] ------------------------------------------------------- @@ -426,7 +430,9 @@ end If you create an association some time after you build the underlying model, you need to remember to create an +add_column+ migration to provide the necessary foreign key. -Second, 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 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. +==== 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 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. 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". -- cgit v1.2.3