# Associations support in ActiveResource?

**URL:** <https://www.ruby-forum.com/t/associations-support-in-activeresource/76732>\
**Category:** Rails\
**Created:** [November 20, 2006, 9:22pm UTC](https://www.ruby-forum.com/t/associations-support-in-activeresource/76732 "2006-11-20T21:22:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![donzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@donzo](https://www.ruby-forum.com/u/donzo)\
**Post date:** [November 20, 2006, 9:22pm UTC](https://www.ruby-forum.com/t/associations-support-in-activeresource/76732/1 "2006-11-20T21:22:07Z")

</div>

Hello Rails Fans,  
I’m working with a rails application that makes ActiveResource calls  
to another rails application. Are there plans for supporting  
associations between models in ActiveResource?

The following is an example of code that runs into the current limit of  
ARes.

on the server app there is  
class CatalogEntry \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :resource, :polymorphic =\> true  
end

on the client app there is  
class CatalogEntry \< ActiveResource::Base  
self.site = “[http://localhost:3000/](http://localhost:3000/)”  
end

from the server rails\_root i run script/console

> > CatalogEntry.find(1)  
> > =\> #\<CatalogEntry:0xb78109a4 @attributes={…}\>  
> > CatalogEntry.find(1).resource  
> > =\> #\<Article:0xb7517f2c @attributes={…}\>

from the client rails\_root i run script/console and try:

> > CatalogEntry.find(1)  
> > =\> #\<CatalogEntry:0xb72eae0c @prefix\_options={}, @attributes={…}\>  
> > CatalogEntry.find(1).resource  
> > NoMethodError: undefined method `resource' for #<CatalogEntry:0xb72d3644> from ./script/../config/../config/../vendor/rails/activeresource/lib/active_resource/base.rb:218:in `method\_missing’  
> > from (irb):3

Don

---

<div class="post-metadata">

**Author:** ![donzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@donzo](https://www.ruby-forum.com/u/donzo)\
**Post date:** [November 20, 2006, 9:28pm UTC](https://www.ruby-forum.com/t/associations-support-in-activeresource/76732/2 "2006-11-20T21:28:08Z")

</div>

On 11/20/06, Don P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class CatalogEntry \< ActiveRecord::Base
> 
> > > CatalogEntry.find(1)  
> > > ./script/…/config/…/config/…/vendor/rails/activeresource/lib/active\_resource/base.rb:218:in  
> > > `method\_missing’  
> > > from (irb):3

You may include the resource in your xml response:  
render :xml =\> @catalog\_entry.to\_xml(:include =\> :resource)

jeremy

---

<div class="post-metadata">

**Author:** ![donzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@donzo](https://www.ruby-forum.com/u/donzo)\
**Post date:** [November 20, 2006, 10:20pm UTC](https://www.ruby-forum.com/t/associations-support-in-activeresource/76732/3 "2006-11-20T22:20:18Z")

</div>

On 11/20/06, Jeremy K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You may include the resource in your xml response:  
> render :xml =\> @catalog\_entry.to\_xml(:include =\> :resource)

thanks, that saves me from doing an extra lookup when retrieving a  
model with associated models. what about supporting associations in  
general? something like where model.associated\_models does a RESTful  
find on the server?

It occurs to me that the only restful find is a get request of  
associated\_models.xml, which is a find(:all). client side filtering  
to pick out the ones associated with model is an option but a terrible  
one.

thanks,  
don

---

<div class="post-metadata">

**Author:** ![donzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@donzo](https://www.ruby-forum.com/u/donzo)\
**Post date:** [November 20, 2006, 10:48pm UTC](https://www.ruby-forum.com/t/associations-support-in-activeresource/76732/4 "2006-11-20T22:48:27Z")

</div>

When I first saw ActiveResource presented by DHH I thought to myself:  
wouldn’t it be cool if they used query parameters to provide  
conditional filtering, a la find conditions? I still thank that would  
be a neat approach which meshes well with the purpose for query  
parameters IMO.

Sincerely,  
Anthony

On 11/20/06, Don P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It occurs to me that the only restful find is a get request of  
> associated\_models.xml, which is a find(:all). client side filtering  
> to pick out the ones associated with model is an option but a terrible  
> one.
> 
> thanks,  
> don
> 
> >

–  
Email: removed\_email\_address@domain.invalid  
Cell: 808 782-5046  
Current Location: Melbourne, FL

---

<div class="post-metadata">

**Author:** ![donzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@donzo](https://www.ruby-forum.com/u/donzo)\
**Post date:** [June 5, 2009, 10:20am UTC](https://www.ruby-forum.com/t/associations-support-in-activeresource/76732/5 "2009-06-05T10:20:49Z")

</div>

Hello

I try this belongs\_to and has\_one relationship for ActiveResource, but  
ror complains.  
Then i try :include=\>[:other\_model\_here], this gave me

 Stuff Article Title 

BUT ror complains

> a = Model.find :last  
> ActiveRecord::UnknownAttributeError: unknown attribute: public\_key

public key is attribute of Other\_model\_here

Any suggestion what to do? i need this attribute public\_key and i don’t  
understand why i can’t simple access it  
Model.Other\_model\_here.public\_key

Please help!

---

<div class="post-metadata">

**Author:** ![donzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f07891/32.png) [@donzo](https://www.ruby-forum.com/u/donzo)\
**Post date:** [November 20, 2006, 11:38pm UTC](https://www.ruby-forum.com/t/associations-support-in-activeresource/76732/6 "2006-11-20T23:38:41Z")

</div>

On 11/20/06, Jeremy K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You may include the resource in your xml response:  
> render :xml =\> @catalog\_entry.to\_xml(:include =\> :resource)

question: the association is polymorphic and the to\_xml method wraps  
the attributes in the association name. without the class name its  
impossible to reconstruct the object in this case.

i suggest that to\_xml work like so:  
\<catalog\_entry class=“CatalogEntry”\>  
Stuff  
  
Article Title

i post this to see if work is underway but i suspect a patch is what  
is really needed here 🙂

don
