开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜