Rails migration: t.references with alternative name?

Ruby on-RailsAssociationsMigration

Ruby on-Rails Problem Overview


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

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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}

Solution 2 - Ruby on-Rails

You can do it this way:

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

or using t.belongs_to as an alias for t.references

You can't add foreign_key: true to those two references lines. If you want to mark them as foreign keys at the database level you need to have a migration with this:

add_foreign_key :courses, :courses, column: :transferrable_as_id
add_foreign_key :courses, :courses, column: :same_as_id

Update

In Rails 5.1 and above you can add the foreign key in the migration in the create_table block like this:

create_table :courses do |t|
  t.string :name
  t.references :transferrable_as, foreign_key: { to_table: 'courses' }
  t.references :same_as, foreign_key: { to_table: 'courses' }
  t.timestamps
end

Solution 3 - Ruby on-Rails

As an added answer to this question -- the Model should have the following line to complete the association:

	belongs_to :transferrable_as, class_name: "Course"
	belongs_to :same_as, class_name: "Course"

Solution 4 - Ruby on-Rails

I think this thread has a different more Rails-ish way: https://stackoverflow.com/questions/417320/scaffolding-activerecord-two-columns-of-the-same-data-type

In the migration: >t.belongs_to :transferrable_as > >t.belongs_to :same_as

Solution 5 - Ruby on-Rails

I don't think references accepts the :as option, but you can create your columns manually...

create_table :courses do |t| 
  t.string  :name 
  t.integer :course1_id
  t.integer :course2_id 
  t.timestamps 
end 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionthemirrorView Question on Stackoverflow
Solution 1 - Ruby on-RailsRyanView Answer on Stackoverflow
Solution 2 - Ruby on-RailsToby 1 KenobiView Answer on Stackoverflow
Solution 3 - Ruby on-RailsDiego Diaz de BerenguerView Answer on Stackoverflow
Solution 4 - Ruby on-RailsthemirrorView Answer on Stackoverflow
Solution 5 - Ruby on-RailsJu NogueiraView Answer on Stackoverflow