# Polymorphic associations and association proxy

**URL:** <https://www.ruby-forum.com/t/polymorphic-associations-and-association-proxy/130481>\
**Category:** Rails\
**Created:** [February 28, 2008, 5:10pm UTC](https://www.ruby-forum.com/t/polymorphic-associations-and-association-proxy/130481 "2008-02-28T17:10:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ivan\_Ukhov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/977dab/32.png) [@Ivan\_Ukhov](https://www.ruby-forum.com/u/Ivan_Ukhov)\
**Post date:** [February 28, 2008, 5:10pm UTC](https://www.ruby-forum.com/t/polymorphic-associations-and-association-proxy/130481/1 "2008-02-28T17:10:01Z")

</div>

Hello

Suppose there is the following architecture:

class Relation \< ActiveRecord::Base  
belongs\_to :owner, :class\_name =\> ‘Unit’, :foreign\_key =\> :owner\_id  
belongs\_to :property, :class\_name =\> ‘Unit’, :foreign\_key  
=\> :property\_id  
end

class Unit \< ActiveRecord::Base  
belongs\_to :instance, :polymorphic =\> true

has\_many :relations\_to, :class\_name =\> ‘Relation’, :foreign\_key  
=\> :owner\_id  
has\_many :relations\_from, :class\_name =\> ‘Relation’, :foreign\_key  
=\> :property\_id

has\_many :properties, :through =\> :relations\_to  
has\_many :owners, :through =\> :relations\_from  
end

class Task \< ActiveRecord::Base  
has\_one :unit, :as =\> :instance  
end

class Message \< ActiveRecord::Base  
has\_one :unit, :as =\> :instance  
end

So the basic idea is that there are units that can have multiply  
owners and properties among other units, so we have many-to-many  
association of the units table with itself. As you can see task and  
message have polymorphic associations with unit, they are units. I  
wonder if there is an easy way to find all related tasks/messages to  
some unit (as association proxies), for example find all tasks as  
properties of the unit, like:

unit.properties.tasks

I want to do things like:

new\_property\_message = Message.new  
unit.properties.messages \<\< new\_property\_message

new\_owner\_task = Task.new  
unit.owners.tasks \<\< new\_owner\_task

unit.owners.messages.find :all, :conditions =\> { :title =\> ‘hello’ }

Thanks

---

<div class="post-metadata">

**Author:** ![Ivan\_Ukhov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/977dab/32.png) [@Ivan\_Ukhov](https://www.ruby-forum.com/u/Ivan_Ukhov)\
**Post date:** [February 29, 2008, 4:30am UTC](https://www.ruby-forum.com/t/polymorphic-associations-and-association-proxy/130481/2 "2008-02-29T04:30:59Z")

</div>

Ivan Ukhov wrote:

> I want to do things like:
> 
> new\_property\_message = Message.new  
> unit.properties.messages \<\< new\_property\_message
> 
> new\_owner\_task = Task.new  
> unit.owners.tasks \<\< new\_owner\_task
> 
> unit.owners.messages.find :all, :conditions =\> { :title =\> ‘hello’ }
> 
> Thanks

So what happened when you tried it?

Irregardless of polymorphic associations, you can’t associate through  
the \<\< operator (as you did above) if one (or both) of the records are  
new.

ilan

---

<div class="post-metadata">

**Author:** ![Ivan\_Ukhov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/977dab/32.png) [@Ivan\_Ukhov](https://www.ruby-forum.com/u/Ivan_Ukhov)\
**Post date:** [February 29, 2008, 4:32am UTC](https://www.ruby-forum.com/t/polymorphic-associations-and-association-proxy/130481/3 "2008-02-29T04:32:35Z")

</div>

Ilan B. wrote:

> Ivan Ukhov wrote:
> 
> > I want to do things like:
> > 
> > new\_property\_message = Message.new  
> > unit.properties.messages \<\< new\_property\_message
> > 
> > new\_owner\_task = Task.new  
> > unit.owners.tasks \<\< new\_owner\_task
> > 
> > unit.owners.messages.find :all, :conditions =\> { :title =\> ‘hello’ }
> > 
> > Thanks
> 
> So what happened when you tried it?
> 
> Irregardless of polymorphic associations, you can’t associate through  
> the \<\< operator (as you did above) if one (or both) of the records are  
> new.
> 
> ilan

Whoops… I meant you can’t associate through the \<\< operator in a MANY  
TO MANY case if one or both records are new

ilan

---

<div class="post-metadata">

**Author:** ![Ivan\_Ukhov](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/977dab/32.png) [@Ivan\_Ukhov](https://www.ruby-forum.com/u/Ivan_Ukhov)\
**Post date:** [February 29, 2008, 6:00am UTC](https://www.ruby-forum.com/t/polymorphic-associations-and-association-proxy/130481/4 "2008-02-29T06:00:18Z")

</div>

There are no such methods union.[owners | properties].[messages |  
tasks]… i don’t mean only \<\< method (if there’s no such, let it be),  
i mean all the functionality that the association proxy gives (scope  
reading/writing etc)

On Feb 29, 6:30 am, Ilan B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
