aboutsummaryrefslogtreecommitdiffstats
path: root/guides/source/association_basics.md
diff options
context:
space:
mode:
authorRyan Lue <hello@ryanlue.com>2018-01-26 12:26:46 +0800
committerRyan Lue <hello@ryanlue.com>2018-01-26 12:26:46 +0800
commitcb8da7aca4d426058d3c67b8f206224e5856770e (patch)
tree424af142a3e56fd2c38f652bc80e52cb7a975c8a /guides/source/association_basics.md
parent8baca31dbe522cb407f0b3b8c8d3d4a6804e5aed (diff)
downloadrails-cb8da7aca4d426058d3c67b8f206224e5856770e.tar.gz
rails-cb8da7aca4d426058d3c67b8f206224e5856770e.tar.bz2
rails-cb8da7aca4d426058d3c67b8f206224e5856770e.zip
Fix example migrations in Associations guide
Diffstat (limited to 'guides/source/association_basics.md')
-rw-r--r--guides/source/association_basics.md26
1 files changed, 9 insertions, 17 deletions
diff --git a/guides/source/association_basics.md b/guides/source/association_basics.md
index 52c30f226f..f895cadea5 100644
--- a/guides/source/association_basics.md
+++ b/guides/source/association_basics.md
@@ -572,40 +572,32 @@ class Book < ApplicationRecord
end
```
-This declaration needs to be backed up by the proper foreign key declaration on the books table:
+This declaration needs to be backed up by a corresponding foreign key column in the books table. For a brand new table, the migration might look something like this:
```ruby
class CreateBooks < ActiveRecord::Migration[5.0]
def change
create_table :books do |t|
- t.datetime :published_at
- t.string :book_number
- t.integer :author_id
+ t.datetime :published_at
+ t.string :book_number
+ t.references :author
end
end
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.
-
-It's a good practice to add an index on the foreign key to improve queries
-performance and a foreign key constraint to ensure referential data integrity:
+Whereas for an existing table, it might look like this:
```ruby
-class CreateBooks < ActiveRecord::Migration[5.0]
+class AddAuthorToBooks < ActiveRecord::Migration[5.0]
def change
- create_table :books do |t|
- t.datetime :published_at
- t.string :book_number
- t.integer :author_id
- end
-
- add_index :books, :author_id
- add_foreign_key :books, :authors
+ add_reference :books, :author
end
end
```
+NOTE: If you wish to [enforce referential integrity at the database level](/active_record_migrations.html#foreign-keys), add the `foreign_key: true` option to the ‘reference’ column declarations above.
+
#### 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 book of the class names. So a join between author and book models will give the default join table name of "authors_books" because "a" outranks "b" in lexical ordering.