Rails 3 migrations: Adding reference column?

后端 未结 10 2087
余生分开走
余生分开走 2020-11-30 17:15

If I create a new rails 3 migration with (for example)

rails g migration tester title:tester user:references

, everything works fine...howe

10条回答
  •  抹茶落季
    2020-11-30 17:59

    EDIT: This is an outdated answer and should not be applied for Rails 4.x+

    You don't need to add references when you can use an integer id to your referenced class.

    I'd say the advantage of using references instead of a plain integer is that the model will be predefined with belongs_to and since the model is already created and will not be affected when you migrate something existing, the purpose is kind of lost.

    So I would do like this instead:

    rails g migration add_user_id_to_tester user_id:integer
    

    And then manually add belongs_to :user in the Tester model

提交回复
热议问题