I have some ARes models (see below) that I'm trying to use associations with (which seems to be wholly undocumented and maybe not possible but I thought I'd give it a try)
So on my service side, my ActiveRecord object will render something like
render :xml => @group.to_xml(:include => :customers)
(see generated xml below)
The models Group and Customers are HABTM
On my ARes side, I'm hoping that it can see the <customers>
xml attribute and automatically populate the .customers
attribute of that Group object , but the has_many etc methods aren't supported (at least as far as I can tell)
So I'm wondering how ARes does it's reflection on the XML to set the attributes of an object. In AR for instance I could create a def customers=(customer_array)
and set it myself, but this doesn't seem to work in ARes.
One suggestion I found for an "association" is the just have a method
def customers
Customer.find(:all, :conditions => {:group_id => self.id})
end
But this has the disadvantage that it makes a second service call to look up those customers... not cool
I'd like my ActiveResource model to see that the customers attribute in the XML and automatically populate my model. Anyone have any experience with this??
# My Services
class Customer < ActiveRecord::Base
has_and_belongs_to_many :groups
end
class Group < ActiveRecord::Base
has_and_belongs_to_many :customer
end
# My ActiveResource accessors
class Customer < ActiveResource::Base; end
class Group < ActiveResource::Base; end
# XML from /groups/:id?customers=true
<group>
<domain>some.domain.com</domain>
<id type="integer">266</id>
<name>Some Name</name>
<customers type="array">
<customer>
<active type="boolean">true</active>
<id type="integer">1</id>
<name>Some Name</name>
</customer>
<customer>
<active type="boolean" nil="true"></active>
<id type="integer">306</id>
<name>Some Other Name</name>
</customer>
</customers>
</group>