Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails ActiveResource Associations

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>
like image 712
brad Avatar asked Apr 28 '10 22:04

brad


1 Answers

ActiveResource does not support associations. But it doen't prevent you from setting/getting complex data to/from a ActiveResource object. Here is how I would implement it:

Server side model

class Customer < ActiveRecord::Base
  has_and_belongs_to_many :groups
  accepts_nested_attributes_for :groups
end

class Group < ActiveRecord::Base
  has_and_belongs_to_many :customers
  accepts_nested_attributes_for :customers
end

Server side GroupsController

def show
  @group = Group.find(params[:id])
  respond_to do |format|
    format.xml { render :xml => @group.to_xml(:include => :customers) }
  end    
end

Client side model

class Customer < ActiveResource::Base
end

class Group < ActiveResource::Base
end

Client side GroupsController

def edit
  @group = Group.find(params[:id])
end

def update
  @group = Group.find(params[:id])
  if @group.load(params[:group]).save
  else
  end
end

Client View: Accessing customer from Group object

# access customers using attributes method. 
@group.customers.each do |customer|
  # access customer fields.
end

Client side: Setting customer to Group object

group.attributes['customers'] ||= [] # Initialize customer array.
group.customers << Customer.build
like image 110
Harish Shetty Avatar answered Sep 20 '22 23:09

Harish Shetty