【发布时间】:2015-08-11 08:11:52
【问题描述】:
如何通过添加外键来分配不同的表名。例如
我有一个类似的模型
class MyPost < ActiveRecord::Base
has_many :comments, class_name: PostComment
end
class PostComment < ActiveRecord::Base
belongs_to :post, class_name: MyPost
end
现在我想像这样更改我的迁移文件:
class CreatePostComments < ActiveRecord::Migration
def change
create_table :post_comments do |t|
t.belongs_to :post, index: true
t.timestamps null: false
end
add_foreign_key :post, :class_name => MyPost
end
end
但它不起作用。迁移被取消。如何更改我的迁移文件以使用我的模型结构。
【问题讨论】:
-
t.belongs_to :post, index: true为您创建了外键。为什么你又在尝试?我没有得到你想要做的事情.. -
您的迁移产生的错误是什么?
标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 rails-migrations