diff options
Diffstat (limited to 'guides/source/migrations.md')
-rw-r--r-- | guides/source/migrations.md | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/guides/source/migrations.md b/guides/source/migrations.md index f2aa72492f..cefbc3b829 100644 --- a/guides/source/migrations.md +++ b/guides/source/migrations.md @@ -79,7 +79,7 @@ end Alternatively, you can use `up` and `down` instead of `change`: -``ruby +```ruby class ChangeProductsPrice < ActiveRecord::Migration def up change_table :products do |t| @@ -202,6 +202,25 @@ end This migration will create a `user_id` column and appropriate index. +There is also a generator which will produce join tables if `JoinTable` is part of the name: + +```bash +rails g migration CreateJoinTableCustomerProduct customer product +``` + +will produce the following migration: + +```ruby +class CreateJoinTableCustomerProduct < ActiveRecord::Migration + def change + create_join_table :customers, :products do |t| + # t.index [:customer_id, :product_id] + # t.index [:product_id, :customer_id] + end + end +end +``` + ### Model Generators The model and scaffold generators will create migrations appropriate for adding @@ -455,7 +474,7 @@ class ExampleMigration < ActiveRecord::Migration t.references :category end - #add a foreign key + # add a foreign key execute <<-SQL ALTER TABLE products ADD CONSTRAINT fk_products_categories @@ -992,7 +1011,7 @@ with foreign key constraints in the database. Although Active Record does not provide any tools for working directly with such features, the `execute` method can be used to execute arbitrary SQL. You -could also use some plugin like +could also use some gem like [foreigner](https://github.com/matthuhiggins/foreigner) which add foreign key support to Active Record (including support for dumping foreign keys in `db/schema.rb`). |