简体   繁体   English

对模型使用has_and_belongs_to_many是一个坏主意吗?

[英]is using has_and_belongs_to_many for models is a bad idea?

有人可以解释一下has_many:through和has_and_belongs_to_many之间的利弊吗?

There is nothing bad about using habtm per se. 使用habtm本身没有什么不好。 The reason why many people don't use this kind of association is that they use has_many :through instead. 许多人不使用这种关联的原因是,他们使用has_many:through代替。 Why? 为什么? Because it's more versatile. 因为它更通用。 While HABTM "hides" the intermediary table, when using has_many :through the middle man is a resource by itself - which is usually a good thing (if for nothing, you can timestamp the relationship). 尽管HABTM“隐藏”了中间表,但在使用has_many时:通过中间人本身就是一种资源-这通常是一件好事(如果没有用,您可以为关系加盖时间戳)。 You'll come across many situations when you'll need to add some behavior or attributes to such a relationship (when designing a system in a resource-oriented fashion). 当您需要为这种关系添加一些行为或属性时(会以面向资源的方式设计系统时),您会遇到很多情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 rails 4-使用has_and_belongs_to_many关联 - rails 4 - using has_and_belongs_to_many association 包含/与has_and_belongs_to_many结合并在两个模型上排序/排序 - includes / joins with has_and_belongs_to_many and sort / ordering on both models 如何为现有模型的has_and_belongs_to_many关联分配值? - How to assign values to has_and_belongs_to_many association of existing models? Ruby on Rails 3:has_and_belongs_to_many两个不同的模型 - Ruby on Rails 3: has_and_belongs_to_many two distinct models 从.csv文件在has_and_belongs_to_many模型中创建记录 - Create records in has_and_belongs_to_many models from .csv file 如何在Rails has_and_belongs_to_many模型中验证提交的ID? - How to validate submitted ids in Rails has_and_belongs_to_many models? ActiveRecord has_and_belongs_to_many:查找具有所有给定元素的模型 - ActiveRecord has_and_belongs_to_many: find models with all given elements RoR:通过has_and_belongs_to_many关联了两个现有模型 - RoR: Associated two existing models via has_and_belongs_to_many has_and_belongs_to_many,两个模型都有下划线名称 - has_and_belongs_to_many where both models have underscore names 如何使用has_and_belongs_to_many将新模型与现有模型相关联 - How to associate a new model with existing models using has_and_belongs_to_many
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM