开发者

Rails migration to reference a User, how to add this foreign key?

开发者 https://www.devze.com 2023-01-28 10:59 出处:网络
I have a 2 Models, User and Post. I want the Post model to have a User in it i.e. add a user_id in the Posts table.

I have a 2 Models, User and Post.

I want the Post model to have a User in it i.e. add a user_id in the Posts table.

What will my migration look like? Or do I just do this manually like:

add_column :posts, :user_id:integer
开发者_开发百科


yep, just do it manually. It'll look like this:

class AddUserIDToPosts < ActiveRecord::Migration
  def self.up
    add_column :posts, :user_id, :integer
  end

  def self.down
    remove_column :posts, :user_id
  end
end
0

精彩评论

暂无评论...
验证码 换一张
取 消