While working on a Rails 4 application, I came across a new addition to migrations
add_reference makes it easier to create migrations for foreign keys.
In Rails 3, to add a foreign key and an index you would write a migration like this:
1 2 3 4 5 6 class AddUserToBooks < ActiveRecord::Migration def change add_column :books, :user_id, :integer add_index :books, :user_id end end
It requires two method calls, one to add the
user_id column to the Books table, and another to add an index to
In Rails 4, you can accomplish the same thing with
1 2 3 4 5 class AddUserToBooks < ActiveRecord::Migration def change add_reference :books, :user, index: true end end
While it isn’t a major change, it further illustrates the foundation that Ruby was built upon.
“Ruby is designed to make programmers happy.” - Yukihiro Matsumoto