has_and_belongs_to_many 连接表的 Rails 迁移
如何执行脚本/生成迁移
来为has_and_belongs_to_many
关系创建联接表?
该应用程序在 Rails 2.3.2 上运行,但我还安装了 Rails 3.0.3。
How do I do a script/generate migration
to create a join table for a has_and_belongs_to_many
relationship?
The application runs on Rails 2.3.2, but I also have Rails 3.0.3 installed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
其中:
和
对于导轨 4:
对于导轨 3:
对于导轨 3
(请注意,表名称按字母顺序列出了两个连接表)
,然后仅对于 Rails 3 及更低版本,您需要编辑生成的迁移,以便不会创建 id 字段:
Where:
and
for rails 4:
for rails 3:
for rails < 3
(note the table name lists both join tables in alphabetical order)
and then for rails 3 and below only, you need to edit your generated migration so an id field is not created:
has_and_belongs_to_many
表必须符合此格式。我假设has_and_belongs_to_many
连接的两个模型已经在数据库中:apples
和oranges
:如果您使用
:独特=> true
在索引上,那么你应该(在rails3中)传递:uniq =>; true
到has_and_belongs_to_many
。更多信息:Rails 文档
更新于 2010-12-13 我已经更新它以删除 id 和时间戳...基本上
ma11hew28
和nunopolonia
是正确的:不能有 id,也不能有时间戳或 Rails 不允许has_and_belongs_to_many
工作。A
has_and_belongs_to_many
table must match this format. I'm assuming the two models to be joined byhas_and_belongs_to_many
are already in the DB :apples
andoranges
:If you use the
:unique => true
on the index, then you should (in rails3) pass:uniq => true
tohas_and_belongs_to_many
.More information: Rails Docs
UPDATED 2010-12-13 I've updated it to remove the id and timestamps... Basically
ma11hew28
andnunopolonia
are correct: There must not be an id and there must not be timestamps or rails won't allowhas_and_belongs_to_many
to work.您应该按字母顺序将要连接的 2 个模型的名称命名为表
并将两个模型 ID 放入表中。
然后将每个模型相互连接,在模型中创建关联。
这是一个例子:
但这不是很灵活,你应该考虑使用 has_many :through
You should name the table the names of 2 models you want to connect by alphabetical order
and put the two model id's in the table.
Then connect each model to each other creating the associations in the model.
Here's an example:
But this is not very flexible and you should think about using has_many :through
最上面的答案显示了一个综合索引,我认为该索引不会用于从橙子中查找苹果。
我确实发现基于“The Doctor What”的答案很有用,而且讨论当然也是如此。
The top answer shows a composite index that I don't believe will be used to lookup apples from oranges.
I did find the answer this is based on by 'The Doctor What' useful and the discussion certainly so too.
在 Rails 4 中,您可以简单地使用
create_join_table :table1s, :table2s
就可以了。
注意:您必须提供包含字母数字的 table1、table2。
In rails 4, you can simple use
create_join_table :table1s, :table2s
it is all.
Caution: you must offord table1, table2 with alphanumeric.
Associations Rails 指南 很棒,但没有准确解释如何创建连接表。
不过,Migrations Rails 指南解释了如何进行联接桌子:
This HABTM section of the Associations Rails Guide is great, but doesn't explain exactly how to create a join table.
However, the Migrations Rails Guide explains how to make a join table:
我喜欢这样做:
rails g migration CreateJoinedTable model1:references model2:references
。这样我就得到了如下所示的迁移:我喜欢在这些列上建立索引,因为我经常使用这些列进行查找。
I like doing:
rails g migration CreateJoinedTable model1:references model2:references
. That way I get a migration that looks like this:I like having index on these columns because I'll often be doing lookups using these columns.