Adding a Model Reference to existing Rails model

后端 未结 3 1779
情深已故
情深已故 2021-02-01 04:15

I\'d like to know the \"proper\" way to approach adding a relation between two existing classes in Rails 3.

Given existing models: Clown & Rabbit

I\

3条回答
  •  情话喂你
    2021-02-01 04:43

    After you set belongs_to in Rabbit, and has_many in Clown, you can do a migration with:

    add_column :rabbit, :clown_id, :integer
    

    EDIT: See Paulo's answer below for a more updated answer (Rails 4+)

提交回复
热议问题