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:
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
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.
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.
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