views:

388

answers:

2

I have two models: Company and User they have a has_and_belongs_to_many relationship.

I'm using active resource with a method to get all users of a company:

def users
  @company = Company.find( params[:id], :include => [:users] )
  render :xml => @company.users.to_xml(:include =>[:companies])   
end

The only problem is, on the other end, when I call my lookup_users method, the returned val is an array Company objects, each with one company attribute as opposed to an array of User objects, each with one company attribute See the output below:

Does anyone know how I can explicitly set the object type to be made by the return with activeresource?

It's odd, because calling @company.users.to_xml in my service renders xml that seems to be normal: Note the "<users type='array'>", but when it's returned, it's a company object.

"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<users type=\"array\">\n  <user>\n    <

the returned value of the above service call: (rdb:1) pp users

[#<AsClient::Company:0x1959628   @attributes=    {"work_phone"=>nil,
    "city"=>nil,
    "fax_number"=>nil,
    "company"=>nil,
    "updated_at"=>Wed Mar 25 21:05:43 UTC 2009,
    "postal_code"=>nil,
    "title"=>nil,
    "the_parent_record_id"=>"650",
    "hashed_password"=>"d80052727e9719113277bcc712d647aedefaff4b",
    "last_logged_into"=>nil,
    "last_seen_at"=>nil,
    "customers"=>
     [#<AsClient::Company:0x19568c4
       @attributes=
        {"name"=>"hmvc0joq",
         "updated_at"=>Wed Mar 25 21:05:43 UTC 2009,
         "main_contact"=>nil,
         "id"=>650,
         "customer_id"=>"650",
         "deleted"=>nil,
         "deleted_by"=>nil,
         "user_id"=>"3263",
         "created_at"=>Wed Mar 25 21:05:43 UTC 2009,
         "active"=>nil},
       @prefix_options={}>],
    "created_by"=>"AsTesting",
    "password_salt"=>"m8j35JAnpd0IlllSHDCfd0BE5R1UHoT1",
    "country"=>nil,
    "activation_code"=>"8xB2J7LL3tQuK2Z9I1AKhAcotaSp8zgz",
    "id"=>3263,
    "updated_by"=>nil,
    "password_reset_code"=>nil,
    "address_1"=>nil,
    "home_phone"=>nil,
    "deleted"=>false,
    "address_2"=>nil,
    "deleted_by"=>nil,
    "first_name"=>"Joe",
    "last_name"=>"Test",
    "province"=>nil,
    "mobile_phone"=>nil,
    "suffix"=>nil,
    "alt_email"=>nil,
    "created_at"=>Wed Mar 25 21:05:43 UTC 2009,
    "email"=>"[email protected]",
    "active"=>false,
    "middle_name"=>nil},   @prefix_options={}>]
A: 

I don't really understand what is your problem. In your example @company is object, but @company.users is array of user objects.

When you call:

@company.users.to_xml(:include =>[:companies])

this will probably render array of users and for every user it will include array of companies.

klew
On the service side, @company.users.to_xml(:include =>[:companies]) renders the xml as an array of users. However on the client side, when I receive this response, It's now an array of Companies. See the above code [#<AsClient::Company ...
brad
Again, I want an array of users, not sure why I all of a sudden get companies, I'm responding with this call: render :xml => @company.users.to_xml(:include =>[:companies])
brad
A: 

Oops, I definitely found my problem. My client is calling

users = Company.find(:all, :params => {:id => company_id}, :from => :users)

Which obviously returns a Company object...

I need more sleep

brad