Adding foreign key to a rails model
The currently accepted answer on this isn’t really accurate as it doesn’t add a database foreign key. It’s just adding integer columns. In Rails 4.2.x, the current approach is: http://guides.rubyonrails.org/active_record_migrations.html#foreign-keys Create a migration: rails generate migration migration_name For existing columns, in the migration add the foreign keys like this: class MigrationName < ActiveRecord::Migration def change … Read more