In Rails 4.2+ you can also set foreign keys in the db as well, which is a great idea.
For simple associations this can be done also on t.references
adding foreign_key: true
, but in this case you'll need two lines.
# The migration
add_reference :posts, :author, references: :users, index: true
add_foreign_key :posts, :users, column: :author_id
# The model
belongs_to :author, class_name: "User"
from this SO answer