Add a reference column migration in Rails 4

前端 未结 7 1826
时光取名叫无心
时光取名叫无心 2020-11-28 00:07

A user has many uploads. I want to add a column to the uploads table that references the user. What should the migration look like?

Here i

7条回答
  •  一生所求
    2020-11-28 00:45

    Rails 5

    You can still use this command to create the migration:

    rails g migration AddUserToUploads user:references
    

    The migration looks a bit different to before, but still works:

    class AddUserToUploads < ActiveRecord::Migration[5.0]
      def change
        add_reference :uploads, :user, foreign_key: true
      end
    end
    

    Note that it's :user, not :user_id

提交回复
热议问题