Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Polymorphic Association with multiple associations on the same model

My question is essentially the same as this one: Polymorphic Association with multiple associations on the same model

However, the proposed/accepted solution does not work, as illustrated by a commenter later.

I have a Photo class that is used all over my app. A post can have a single photo. However, I want to re-use the polymorphic relationship to add a secondary photo.

Before:

class Photo     belongs_to :attachable, :polymorphic => true end  class Post    has_one :photo, :as => :attachable, :dependent => :destroy end 

Desired:

class Photo     belongs_to :attachable, :polymorphic => true end  class Post    has_one :photo,           :as => :attachable, :dependent => :destroy    has_one :secondary_photo, :as => :attachable, :dependent => :destroy end 

However, this fails as it cannot find the class "SecondaryPhoto". Based on what I could tell from that other thread, I'd want to do:

   has_one :secondary_photo, :as => :attachable, :class_name => "Photo", :dependent => :destroy 

Except calling Post#secondary_photo simply returns the same photo that is attached via the Photo association, e.g. Post#photo === Post#secondary_photo. Looking at the SQL, it does WHERE type = "Photo" instead of, say, "SecondaryPhoto" as I'd like...

Thoughts? Thanks!

like image 959
Matt Rogish Avatar asked Mar 22 '10 17:03

Matt Rogish


People also ask

How is polymorphic association set up in Rails?

In Ruby on Rails, a polymorphic association is an Active Record association that can connect a model to multiple other models. For example, we can use a single association to connect the Review model with the Event and Restaurant models, allowing us to connect a review with either an event or a restaurant.

What is polymorphic true 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 are associations in Rails?

Association in Rails defines the relationship between models. It is also the connection between two Active Record models. To figure out the relationship between models, we have to determine the types of relationship. Whether it; belongs_to, has_many, has_one, has_one:through, has_and_belongs_to_many.


1 Answers

I have done that in my project.

The trick is that photos need a column that will be used in has_one condition to distinguish between primary and secondary photos. Pay attention to what happens in :conditions here.

has_one :photo, :as => 'attachable',          :conditions => {:photo_type => 'primary_photo'}, :dependent => :destroy  has_one :secondary_photo, :class_name => 'Photo', :as => 'attachable',         :conditions => {:photo_type => 'secondary_photo'}, :dependent => :destroy 

The beauty of this approach is that when you create photos using @post.build_photo, the photo_type will automatically be pre-populated with corresponding type, like 'primary_photo'. ActiveRecord is smart enough to do that.

like image 147
Max Chernyak Avatar answered Sep 18 '22 19:09

Max Chernyak