[英]Ruby on rails polymorphic associations
Is there a difference between the has_many , has_and_belongs_to_many and the polymorphic association. has_many 、 has_and_belongs_to_many 和多态关联之间有区别吗? Why should I use polymorphic association.
为什么要使用多态关联。
has_many
, has_and_belongs_to_many
and the polymorphic associations are solutions to different problems. has_many
、 has_and_belongs_to_many
和多态关联是不同问题的解决方案。
has_many
means you have a parent object that has many other children. has_many
意味着您有一个父对象,其中包含许多其他子对象。 While the children do only belong to that specific parent.has_and_belongs_to_many
means the objects are not structured in a hierarchical structure. has_and_belongs_to_many
表示对象不是按层次结构构建的。 And that the objects on both sides can be connected to mutiple others.polymorphic associations
are used when the association doesn't point to one specific other model.polymorphic associations
。 An example might be an Author
model that has a created_work
association, but it doesn't point to a decicated CreatedWork
model.created_work
关联的Author
模型,但它并不指向指定的CreatedWork
模型。 But instead, it can return different things, like an instance of a Book
, a Play
an Article
, or a simple Comment
which are all different classes and have different database tables.Book
的实例、 Play
an Article
或简单的Comment
,它们都是不同的类并具有不同的数据库表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.