Rails migration: t.references with alternative name?

themirror picture themirror · May 29, 2010 · Viewed 72.2k times · Source

So I have a create_table like this for Courses at a School:

create_table :courses do |t|
  t.string :name
  t.references :course
  t.timestamps
end

but I want it to reference two other courses like:

has_many :transferrable_as # A Course
has_many :same_as          # Another Course

Can I say the following?

t.references :transferrable_as, :as=> :course

Answer

Ryan picture Ryan · Dec 17, 2016

You can do this all in the initial migration/column definition (at least currently in Rails 5):

t.references :transferable_as, index: true, foreign_key: {to_table: :courses}
t.references :same_as, index: true, foreign_key: {to_table: :courses}