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.