简体   繁体   English

多态关联

[英]Polymorphic association

If you have polymorphic belongs_to associations then references will add both of the columns required:如果您有多态的 belongs_to 关联,那么引用将添加所需的两列:

create_table :products do |t|
  t.references :attachment, :polymorphic => {:default => 'Photo'}
end

will add an attachment_id column and a string attachment_type column with a default value of 'Photo'.将添加一个attachment_id列和一个字符串attachment_type列,默认值为“Photo”。

What, exactly, does this mean?这到底是什么意思?

Here is the documentation on the references method: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html#M001938以下是有关参考方法的文档: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html#M001938

The code of the references method is as follows:引用方法的代码如下:

497:       def references(*args)
498:         options = args.extract_options!
499:         polymorphic = options.delete(:polymorphic)
500:         args.each do |col|
501:           column("#{col}_id", :integer, options)
502:           column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) unless polymorphic.nil?
503:         end
504:       end

As you can see.如你看到的。 It adds both a [col]_id and [col]_type column to the table.它将 [col]_id 和 [col]_type 列添加到表中。

It's the same as saying:这和说的一样:

create_table :products do |t|
  t.integer :attachment_id
  t.string  :attachment_type, :default => 'Photo'
end

Polymorphic associations are used to connect one kind of objects to multiple kinds of other objects.多态关联用于将一种对象连接到多种其他对象。

A good example might be an application that supports tags, where tags can be connected to both Products and Categories .一个很好的例子可能是支持标签的应用程序,其中标签可以连接到ProductsCategories

In your example, it looks like Products could be attached to multiple kinds of objects, where the default kind of object is a Photo.在您的示例中,产品看起来可以附加到多种对象,其中 object 的默认类型是照片。 ( attachment_type would be "Photo", and attachment_id would be an id of a row in the 'photos' table) attachment_type将是“照片”, attachment_id ID 将是“照片”表中一行的 ID)

Basically, polymorphic association by definition adds ability to create associations with many other Rails ActiveRecord Models.基本上,根据定义,多态关联增加了与许多其他 Rails ActiveRecord 模型创建关联的能力。

Both Columns are there for Rails to know which Model the association is referring to, you have the attachment_type column (ie String) (default value is "Photo",table-name: photos in the db), and the attachment_id is sort of like a foreign key to that particular Model/table (ie Photo). Rails 可以使用这两个列来知道关联所指的是哪个 Model,您有 attachment_type 列(即字符串)(默认值为“照片”,表名:数据库中的照片),并且 attachment_id 有点像该特定模型/表的外键(即照片)。

Usually this provides you flexibility to associate one Model to many others.通常,这为您提供了将一个 Model 与许多其他关联的灵活性。

polymorphic means that it can belong to different objects (or, different records in different tables).多态意味着它可以属于不同的对象(或者,不同表中的不同记录)。 The way it determines this is based on the type and id fields.它确定这一点的方式是基于 type 和 id 字段。 If your association was not polymorphic it would only have an _id field.如果您的关联不是多态的,它将只有一个 _id 字段。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM