Rails Migration: add_reference to Table but Different Column Name For Foreign Key Than Rails Convention Rails Migration: add_reference to Table but Different Column Name For Foreign Key Than Rails Convention ruby-on-rails ruby-on-rails

Rails Migration: add_reference to Table but Different Column Name For Foreign Key Than Rails Convention


in rails 5.x you can add a foreign key to a table with a different name like this:

class AddFooBarStoreToPeople < ActiveRecord::Migration[5.0]  def change    add_reference :people, :foo_bar_store, foreign_key: { to_table: :stores }  endend

Inside a create_table block

t.references :feature, foreign_key: {to_table: :product_features}


In Rails 4.2, you can also set up the model or migration with a custom foreign key name. In your example, the migration would be:

class AddReferencesToPeople < ActiveRecord::Migration  def change    add_column :people, :foo_bar_store_id, :integer, index: true    add_foreign_key :people, :stores, column: :foo_bar_store_id  endend

Here is an interesting blog post on this topic. Here is the semi-cryptic section in the Rails Guides. The blog post definitely helped me.

As for associations, explicitly state the foreign key or class name like this (I think your original associations were switched as the 'belongs_to' goes in the class with the foreign key):

class Store < ActiveRecord::Base  has_one :person, foreign_key: :foo_bar_store_idendclass Person < ActiveRecord::Base  belongs_to :foo_bar_store, class_name: 'Store'end

Note that the class_name item must be a string. The foreign_key item can be either a string or symbol. This essentially allows you to access the nifty ActiveRecord shortcuts with your semantically-named associations, like so:

person = Person.firstperson.foo_bar_store# returns the instance of store equal to person's foo_bar_store_id

See more about the association options in the documentation for belongs_to and has_one.


To expand on schpet's answer, this works in a create_table Rails 5 migration directive like so:

create_table :chapter do |t|  t.references :novel, foreign_key: {to_table: :books}  t.timestampsend