I have 3 models: Question, Option, Rule
Question has_many options; Option needs a foreign key for question_id
Rule table consists of 3 foreign_keys:
- 2 columns/references to question_ids -> foreign keys named as 'assumption_question_id' and 'consequent_question_id'
- 1 column/reference to option_id -> foreign key named as option_id or condition_id
Associations for Rule: Question has_many rules; and Option has_one rule
I want to understand how to write up migrations for this, and how that associates to the 'has_many'/'belongs_to' statements I write up in my model, and the ':foreign_key' option I can include in my model.
I had this for my Option migration, but I'm not sure how the "add_index" statement works in terms of foreign keys, and how I can use it for my Rule migration: (my Question and Options models have appropriate has_many and belongs_to statements - and work fine)
class CreateOptions < ActiveRecord::Migration
def change
create_table :options do |t|
t.integer :question_id
t.string :name
t.integer :order
t.timestamps
end
add_index :options, :question_id
end
end
Thank you for the help!