I want to make a migration
in Rails, referencing another table. Usually, I would do something like:
add_column :post, :user, :references
This creates a column named user_id
in posts
table. But what if, instead of user_id
, I want something like author_id
? How can I do that?
Best Answer
For Rails 5+
Initial Definition:
If you are defining your
Post
model table, you can setreferences
,index
andforeign_key
in one line:Update Existing:
If you are adding references to an existing table, you can do this:
Note: The default value for
index
is true.