如何使用外键创建迁移脚本

时间:2013-02-01 06:52:41

标签: activerecord ruby-on-rails-2

我想通过迁移脚本创建带有外键的表。

create_table :posts do |t|
  t.string :title
end

create_table :comments do |t|
  t.references :post
end

rake db:migrate之后,评论未通过id传播。

如何在rails-2.2.3中创建它?

1 个答案:

答案 0 :(得分:0)

您可以将迁移创建为普通的表模式,并使用:belongs_to选项进行belongs_to,以便更广泛地支持旧模式和使用单独的外键:

belongs_to :posts, :foreign_key => 'post_id'

相关问题