Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Polymorphic association

If you have polymorphic belongs_to associations then references will add both of the columns required:

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’.

What, exactly, does this mean?

like image 586
Arc Avatar asked Mar 31 '09 05:03

Arc


People also ask

What is polymorphic association in Rails?

Polymorphic relationship in Rails refers to a type of Active Record association. This concept is used to attach a model to another model that can be of a different type by only having to define one association.

What's a polymorphic relationship?

So what is a polymorphic relationship? A polymorphic relationship is where a model can belong to more than one other model on a single association. To clarify this, let's create an imaginary situation where we have a Topic and a Post model. Users can leave comments on both topics and posts.

What is polymorphic association in Sequelize?

A polymorphic association consists on two (or more) associations happening with the same foreign key. For example, consider the models Image , Video and Comment . The first two represent something that a user might post. We want to allow comments to be placed in both of them.

How is polymorphic association set up in Rails?

The basic structure of a polymorphic association (PA)sets up 2 columns in the comment table. (This is different from a typical one-to-many association, where we'd only need one column that references the id's of the model it belongs to). For a PA, the first column we need to create is for the selected model.


1 Answers

Here is the documentation on the references method: 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.

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.

In your example, it looks like Products could be attached to multiple kinds of objects, where the default kind of object is a Photo. (attachment_type would be "Photo", and attachment_id would be an id of a row in the 'photos' table)

like image 179
Gdeglin Avatar answered Oct 07 '22 23:10

Gdeglin