Reference with custom name and foreign key

Viewed 4403

I'm on Rails 5 and have a User model.

I want to create a Book model, referencing the User model with the name author. I want as well to set foreign keys in the migration.

When searching an answer I have only found how to add columns in a migration, not on creating a new table. How would the below look like for create_table :books?

add_reference :books, :author, references: :users, index: true
add_foreign_key :books, :users, column: :author_id
1 Answers
Related