如何通过 has_and_belongs_to_many 迁移到 has_many? [英] How to migrate has_and_belongs_to_many to has_many through?
问题描述
我们在两个模型之间有一个简单的 has_and_belongs_to_many 关系.我们想向该模型添加一些参数,因此我们需要将其更改为 has_many :through 类型的模型.
We have a simple has_and_belongs_to_many relation between two models. We would like to add in some paramaters to that model, so we need to change it to a has_many :through sort of model.
据我所知,我们需要添加一个 id 列(以及我们额外想要的任何列).但是,我不是 100% 清楚如何做到这一点.如果我们添加一个整数列:id,rails 会知道那是 'id' 主键吗?
As I know it, we need to add in an id column (along with whatever columns we want additionally). However, I'm not clear 100% on how to do this. If we add an integer column :id, will rails know that that is the 'id' primary key?
我们使用的是最新的 3.x.
We're using the latest 3.x.
推荐答案
只需在迁移时在表上添加 id:
Just add id on your table in migration:
add_column :table, :id, :primary_key
引用自这个答案
这篇关于如何通过 has_and_belongs_to_many 迁移到 has_many?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!