Bạn có thể làm theo cách này:
create_table :courses do |t|
t.string :name
t.references :transferrable_as
t.references :same_as
t.timestamps
end
hoặc sử dụng t.belongs_to làm bí danh chot.references
Bạn không thể thêm foreign_key: truevào hai dòng tham chiếu đó. Nếu bạn muốn đánh dấu chúng là khóa ngoại ở cấp cơ sở dữ liệu, bạn cần phải di chuyển với điều này:
add_foreign_key :courses, :courses, column: :transferrable_as_id
add_foreign_key :courses, :courses, column: :same_as_id
Cập nhật
Trong Rails 5.1 trở lên, bạn có thể thêm khóa ngoại khi di chuyển trong create_tablekhối như sau:
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