Add a reference column migration in Rails 4

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 is what I have. I’m not sure if I should use (1) :user_id, :int or (2) :user, :references. I’m not even sure if (2) works. Just trying to do this the “rails” way.

class AddUserToUploads < ActiveRecord::Migration
  def change
    add_column :uploads, :user_id, :integer
  end
end

Relevant question except for Rails 3. Rails 3 migrations: Adding reference column?

7 Answers
7

Leave a Comment