# Returning associated ActiveRecord data through a Rails web s

**URL:** <https://www.ruby-forum.com/t/returning-associated-activerecord-data-through-a-rails-web-s/108069>\
**Category:** Rails\
**Created:** [June 28, 2007, 8:56pm UTC](https://www.ruby-forum.com/t/returning-associated-activerecord-data-through-a-rails-web-s/108069 "2007-06-28T20:56:26Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![willbailey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e9c0ed/32.png) [@willbailey](https://www.ruby-forum.com/u/willbailey)\
**Post date:** [June 28, 2007, 8:56pm UTC](https://www.ruby-forum.com/t/returning-associated-activerecord-data-through-a-rails-web-s/108069/1 "2007-06-28T20:56:26Z")

</div>

I have built a web service to allow people to query and manipulate my  
ActiveRecord data.

Example:  
api\_method :find\_widget\_by\_id,  
:expects =\> [:int],  
:returns =\> [Widget]

This works fairly well, since it returns my ActiveRecord “widget” class

- no  
mess, no fuss, no hassle, no repetition. The only problem is, this  
class  
interacts with several other objects:  
has\_and\_belongs\_to\_many :associatedthings  
belongs\_to :category  
belongs\_to :otherthing  
belongs\_to :thirdthing

I would ideally like to return the entire object hierarchy as a SOAP or  
XML-RPC object. I have tried this in my service controller:  
def find\_widget\_by\_id( id )  
Widget.find( id, :include =\> [:associatedthings] )  
end

But it still just returns the “Widget” object without any of the  
“associatedthing” objects. I have tried it with one of the “belongs\_to”  
objects as well, and the results are the same. Has anyone done this  
before? Is there any kind of Rails magic to do this, or should I build  
some  
custom return classes and shove all the data into those? I’m trying to  
follow the “DRY” principle in order to keep this as simple as possible,  
even  
though the brute force option would work.
