1

i understand how to define simple relations between two tables as discussed here:

http://guides.rubyonrails.org/getting_started.html#adding-a-second-model

and here: Rails 4.2 foreign key

but, how does one define multiple references to the same model in a migration? say for example using the prior posts:

add_foreign_key :articles, :authors

and that you want to have both a reference to the author, and say the editor who is also an author. which would end up with something like this in the model in prior versions of rails:

belongs_to :editor, :class_name => "Author", :foreign_key => "editor_id"

furthermore is there a syntax for defining this at the rails g scaffold level or would i have to modify the migration after?

Community
  • 1
  • 1
boar
  • 93
  • 1
  • 8

1 Answers1

1

That would just be this:

add_column :articles, :author_id, :integer
add_column :articles, :editor_id, :integer
add_foreign_key :articles, :authors
add_foreign_key :articles, :authors, column: :editor_id, primary_key: "id"
Rob Wise
  • 4,930
  • 3
  • 26
  • 31
  • with one change this works. authors pk is "id" so the primary_key: "author_id" changed to primary_key: "id" seemed to do the right thing. also using t.references for the column in the table definition was fine also – boar Dec 27 '14 at 12:48