1

I have a model called WalksSection and another called WalkSectionButton. I have created the WalksSection model (and done migration).

Now I want to create WalkSectionButton but want to create two references between the two tables:

walk_sections.id = walk_section_buttons.section_button_id
walk_sections.id = walk_section_buttons.next_section_button_id 

The first is the standard one but the other (I think) I need to tell the migration and the model the column name.

So the standard one in the model is

  belongs_to :WalkSection

and in the migration is

  t.references :WalkSection, foreign_key: true

So I guess I need to have a second line in model and migration which specifies the foreign (and primary) key name but not sure of the syntax.

The other way to do this maybe is to run the migration to create the first reference then do another migration that adds the column and key for the second foregn key.

add_column      :walk_section_buttons, :next_section_button_id,  :integer
add_foreign_key :walk_section_buttons, :walk_sections, column: :next_section_button_id, primary_key: :id

But not sure what to do to the model. Maybe I don't need to but in the WalksSections model I think I should add a has_many, but again would need to specify a column name for the second one.

Ben Edwards
  • 425
  • 5
  • 18

0 Answers0