# Convert between two AssociationProxies

**URL:** <https://www.ruby-forum.com/t/convert-between-two-associationproxies/130487>\
**Category:** Rails\
**Created:** [February 28, 2008, 6:01pm UTC](https://www.ruby-forum.com/t/convert-between-two-associationproxies/130487 "2008-02-28T18:01:52Z")\
**Posts on this page:** 6\
**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, 6:01pm UTC](https://www.ruby-forum.com/t/convert-between-two-associationproxies/130487/1 "2008-02-28T18:01:52Z")

</div>

Hello

There’s a polymorphic association Person -\> Adult. There’s an  
AssociationProxy array of Adult, how to convert this proxy of Person  
to corresponding AssociationProxy of Adult?

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 28, 2008, 6:41pm UTC](https://www.ruby-forum.com/t/convert-between-two-associationproxies/130487/2 "2008-02-28T18:41:00Z")

</div>

Normally you operate (iterate) over one of the proxies since it’s job  
really is to give you a collection (array) of model instances.

Can you give a more concrete example of what you’re trying to  
accomplish?

---

<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, 7:23pm UTC](https://www.ruby-forum.com/t/convert-between-two-associationproxies/130487/3 "2008-02-28T19:23:07Z")

</div>

Let me give you another example, there’s a model Thing that has  
polymorphic association with GoodThing and BadThing. Thing also has  
many-to-many association with itself: Thing can have many owners and  
many properties among other Things.

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

class Thing \< 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 GoodThing \< ActiveRecord::Base  
has\_one :thing, :as =\> :instance  
end

class BadThing \< ActiveRecord::Base  
has\_one :thing, :as =\> :instance  
end

I want to perform operation like:

new\_good = GoodThing.new  
some\_thing.properties.good\_things \<\< new\_good

new\_bad = BadThings.new  
some\_thing.owners.bad\_things \<\< new\_bad

some\_thing.owners.find :all, :condition =\> {…}

or even

some\_[good | bad]\_thing.[owners | properties] \<\< another\_thing

I found out that there is a nice plugin has\_finder:

[http://pivots.pivotallabs.com/users/nick/blog/articles/284-hasfinder-it-s-now-easier-than-ever-to-create-complex-re-usable-sql-queries](http://pivots.pivotallabs.com/users/nick/blog/articles/284-hasfinder-it-s-now-easier-than-ever-to-create-complex-re-usable-sql-queries)

so I can add to Thing

class Thing  
has\_finder :bad\_things, :conditions =\> { :instance\_type =\>  
‘BadThing’ }  
has\_finder :good\_things, :conditions =\> { :instance\_type =\>  
‘GoodThing’ }  
end

and write something like

some\_thing.owners.bad\_things \<\< new\_thing

The problem is that these AssociationProxies (Thing.bad\_things,  
Thing.good\_things) contain objects of class Thing, not BadThing or  
GoodThing and I need to do something like

new\_good\_thing = GoodThing.new  
new\_thing = Thing.new  
new\_thing.instance = new\_good\_thing

some\_thing.owners \<\< new\_thing

And I wanna operate directly with an AssociationProxy of BadThing or  
GoodThings…

something like that)) i hope you can get what i mean

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, 8:56pm UTC](https://www.ruby-forum.com/t/convert-between-two-associationproxies/130487/4 "2008-02-29T20:56:11Z")

</div>

i dont want to create any relationships with Property, but between two  
Subproperties, like some Subproperty can own many others and be owned  
by many others: :property -\> :owner, :subproperty -\> :possession

---

<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, 9:06pm UTC](https://www.ruby-forum.com/t/convert-between-two-associationproxies/130487/5 "2008-02-29T21:06:51Z")

</div>

Actually what i want is to connect several models with many-to-many  
owner/property relationships, there should be only one model  
Relationship that holds all the relations, with following columns:

owner\_id  
owner\_type  
property\_id  
property\_type  
created\_on  
etc

and i want to easy access to all owners/properties with specific type  
of some object:

an\_instance\_of\_one\_model.all\_owners\_of\_this\_instance\_but\_from\_another\_model  
\<\< new\_instance\_of\_that\_another\_model

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, 5:15pm UTC](https://www.ruby-forum.com/t/convert-between-two-associationproxies/130487/6 "2008-02-29T17:15:12Z")

</div>

Sounds like you might have a much simpler data model if you use a  
polymorphic association. This will allow you to have a relationship  
to many other properties without regard to what class they are. The  
only attribute that you care about is their relationship.

class Property \< AR::Base

# This is done for relationships back to this class

has\_one :property\_relationship, :as=\>:subproperty

has\_many :property\_relationships

has\_many :good\_properties, :through=\>:property\_relationships,  
:conditions=\>{:subproperty\_type=\>GoodProperty.name}

has\_many :bad\_properties, :through=\>:property\_relationships,  
:conditions=\>{:subproperty\_type=\>BadProperty.name}

has\_many :super\_properties, :through=\>:property\_relationships,  
:conditions=\>{:subproperty\_type=\>Property.name}  
end

class PropertyRelationship \< AR::Base  
belongs\_to :property  
belongs\_to :subproperty, :polymorphic =\> true  
end

class GoodSubproperty \< AR::Base  
has\_one :property\_relationship, :as=\>:subproperty  
end

class BadSubproperty \<\< AR::Base  
has\_one :property\_relationship, :as=\>:subproperty  
end
