Associations and (multiple) foreign keys in rails (3.2) : how to describe them in the model, and write up migrations

前端 未结 3 668
無奈伤痛
無奈伤痛 2020-12-22 03:07

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:

3条回答
  •  情深已故
    2020-12-22 03:16

    You can set a foreign key in your model like this:

    class Leaf < ActiveRecord::Base
    belongs_to :tree, :foreign_key => "leaf_code"
    end
    

    You do not need to specify this in a migration, rails will pull the foreign key from the model class definition.

提交回复
热议问题