如何在添加外键时分配不同的表名。例如
我有一个模型:
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
为你创建了外键。为什么你还要尝试呢?我不明白你在尝试做什么。 - Arup Rakshit