For Rails 5+
Initial Definition:
If you define a table of Post models, you can set references , index and foreign_key on the same line:
t.references :author, index: true, foreign_key: { to_table: :users }
Update existing:
If you add links to an existing table, you can do this:
add_reference :posts, :author, foreign_key: { to_table: :users }
Note: The default value for index is true.
Sheharyar Feb 05 '17 at 19:11 2017-02-05 19:11
source share