Note that there are some explanatory texts on larger screens.

plurals
  1. PORails ActiveResource Associations
    primarykey
    data
    text
    <p>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)</p> <p>So on my service side, my ActiveRecord object will render something like</p> <pre><code>render :xml =&gt; @group.to_xml(:include =&gt; :customers) </code></pre> <p>(see generated xml below)</p> <p>The models Group and Customers are HABTM</p> <p>On my ARes side, I'm hoping that it can see the <code>&lt;customers&gt;</code> xml attribute and automatically populate the <code>.customers</code> attribute of that Group object , but the has_many etc methods aren't supported (at least as far as I can tell)</p> <p>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 <code>def customers=(customer_array)</code> and set it myself, but this doesn't seem to work in ARes. </p> <p>One suggestion I found for an "association" is the just have a method</p> <pre><code>def customers Customer.find(:all, :conditions =&gt; {:group_id =&gt; self.id}) end </code></pre> <p>But this has the disadvantage that it makes a second service call to look up those customers... not cool</p> <p><strong>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??</strong></p> <pre><code># My Services class Customer &lt; ActiveRecord::Base has_and_belongs_to_many :groups end class Group &lt; ActiveRecord::Base has_and_belongs_to_many :customer end # My ActiveResource accessors class Customer &lt; ActiveResource::Base; end class Group &lt; ActiveResource::Base; end # XML from /groups/:id?customers=true &lt;group&gt; &lt;domain&gt;some.domain.com&lt;/domain&gt; &lt;id type="integer"&gt;266&lt;/id&gt; &lt;name&gt;Some Name&lt;/name&gt; &lt;customers type="array"&gt; &lt;customer&gt; &lt;active type="boolean"&gt;true&lt;/active&gt; &lt;id type="integer"&gt;1&lt;/id&gt; &lt;name&gt;Some Name&lt;/name&gt; &lt;/customer&gt; &lt;customer&gt; &lt;active type="boolean" nil="true"&gt;&lt;/active&gt; &lt;id type="integer"&gt;306&lt;/id&gt; &lt;name&gt;Some Other Name&lt;/name&gt; &lt;/customer&gt; &lt;/customers&gt; &lt;/group&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload