associations support in ActiveResource?

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/"
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

You may include the resource in your xml response:
render :xml => @catalog_entry.to_xml(:include => :resource)

jeremy

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

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

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">
<attrib1>Stuff</attrib1>
<resource class="Article">
   <attrib1>Article Title</attrib1>
</resource>
<catalog>

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

don

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

<Model>
<attrib1>Stuff</attrib1>
<Other_model_here class="Article">
   <attrib1>Article Title</attrib1>
</other_model_here>
<catalog>

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!