Ruby-on-rails 我是否需要手动为 HABTM 连接表创建迁移?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/564306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do I need to manually create a migration for a HABTM join table?
提问by Valentin Vasilyev
I'm struggling now to get HATBM working correctly. I have a beaten scanario: articles and tags. I presume, HABTM should be used here, since it is a many-to-many relationship. I don't know however if I should manually create a join table (articles_tags in this case).
我现在正在努力让 HATBM 正常工作。我有一个被打败的 scanario:文章和标签。我认为这里应该使用 HABTM,因为它是多对多的关系。但是,我不知道是否应该手动创建连接表(在这种情况下为 articles_tags)。
My code currently as follows:
我的代码目前如下:
class Article < ActiveRecord::Base
has_and_belongs_to_many :tags
end
class Tag < ActiveRecord::Base
has_and_belongs_to_many :articles
end
When I run the migrations, no 3rd table is created. Also, I would like to add that my third table doesn't bear any domain logic, just blind assignment.
当我运行迁移时,没有创建第三个表。另外,我想补充一点,我的第三个表不承载任何域逻辑,只是盲目分配。
I'm using Rails 2.2.2
我正在使用 Rails 2.2.2
回答by Ryan Bigg
You should do this in a migration of one of the tables, or in a separate migration if those migrations have been ran:
您应该在其中一个表的迁移中执行此操作,或者如果已运行这些迁移,则应在单独的迁移中执行此操作:
create_table :articles_tags, :id => false do |t|
t.references :article, :tag
end
add_index :articles_tags, [:article_id, :tag_id]
This will create the table for you and the :id => falsetells Rails not to add an id field to this table. There's an index also, which will speed up lookups for this join table.
这将为您创建表,并:id => false告诉 Rails 不要向该表添加 id 字段。还有一个索引,它将加快对该连接表的查找。
You could also generate a model (ArticlesTag) for this and do:
您还可以为此生成一个模型(ArticlesTag)并执行以下操作:
# article.rb
has_many :articles_tags
has_many :tags, :through => :articles_tags
# tag.rb
has_many :articles_tags
has_many :articles, :through => :articles_tags
# article_tag.rb
belongs_to :tag
belongs_to :article
And then create the table in the migration generated from the script/generate model articles_tagcall.
然后在script/generate model articles_tag调用生成的迁移中创建表。
回答by Sai Perchard
回答by Hyman Chu
You probably also want to add an index to the migration:
您可能还想为迁移添加索引:
add_index "articles_tags", "article_id"
add_index "articles_tags", "article_id"
add_index "articles_tags", "tag_id"
add_index "articles_tags", "tag_id"
However, if you want tagging functionality I'd recommend the acts_as_taggable_on rails plugin:
但是,如果您想要标记功能,我建议使用acts_as_taggable_on rails 插件:
http://www.intridea.com/tag/acts_as_taggable_onhttp://github.com/mbleigh/acts-as-taggable-on/
http://www.intridea.com/tag/acts_as_taggable_on http://github.com/mbleigh/acts-as-taggable-on/
I've used it on a project and it was very easy to implement.
我已经在一个项目中使用过它,它很容易实现。
One of the issues with a join table for tagging is that it can easily get ugly creating a join table for each content type you wish to make taggable (ie. comments_tags, posts_tags, images_tags, etc). This plugin uses a taggings table which includes a discriminator to determine the content type without the need of a specific join table for each type.
用于标记的连接表的问题之一是,为您希望使其可标记的每种内容类型(即,comments_tags、posts_tags、images_tags 等)创建连接表很容易变得丑陋。这个插件使用一个标签表,其中包括一个鉴别器来确定内容类型,而不需要每个类型的特定连接表。
回答by mArtinko5MB
In combination with this Qeuestion(1st answear) How to set up a typical users HABTM roles relationshipand 1st answear from here, it has to be understood even by a monkey. I am new in RoR and it's got working like a charm
结合这个问题(1st answear)如何建立一个典型的用户HABTM角色关系和1st answear 从这里开始,即使是猴子也必须理解。我是 RoR 的新手,它的工作很有魅力

