I''m currently writing an app that provides a Web Service. I''ve got a model Header and an associated has_one HeaderImage and I''m trying to find a way to make this data available. Defining a normal API: api_method :header, :expects => [{''id'' => :int}], :returns => [Header] Only returns the header object without associations, has anyone got experience with using the Struct class? I''ve got the Rails book but the chapter on using th Struct class is less than comprehensive. -- Simen Brekken
On 7/25/05, Simen Brekken <simen-qaH4BmqzZooOvCyXmLEu7A@public.gmane.org> wrote:> api_method :header, :expects => [{''id'' => :int}], :returns => [Header] > > Only returns the header object without associations, has anyone got > experience with using the Struct class? I''ve got the Rails book but the > chapter on using th Struct class is less than comprehensive.Hi Simen, ActionWebService does not follow associations for ActiveRecord classes currently. I don''t think there are AR-imposed limitations that would prevent following these, its just a type discovery process (needed for WSDL generation) thats a bit more long-winded than the usual, and I didn''t get around to implementing it. Please file a feature request for this, I can see it being a reasonable expectation people might have. The workaround at this time would be to map AR to an ActionWebService::Struct derivative. ::Struct is simple, because all it is is something having members of a particular type, where the type can be anything you can put in as the type of a signature entry, i.e. :int, :string, SomeClass, etc. class Me < ActionWebService::Struct member :id, :int member :name, :string member :emails, [:string] member :address, Address end Leon
leon breedt wrote:> The workaround at this time would be to map AR to an > ActionWebService::Struct derivative.There''s no mention of where to put the Struct class, "model" the appropriate place?> ::Struct is simple, because all it is is something having members of a > particular type, where the type can be anything you can put in as the > type of a signature entry, i.e. :int, :string, SomeClass, etc. > > class Me < ActionWebService::Struct > member :id, :int > member :name, :string > member :emails, [:string] > member :address, Address > endI''ve tried a couple of approaches now. api_method :header, :expects => [{''id'' => :int}], :returns => [HeaderStruct] def header(id) header = Header.find_by_category_id(id) HeaderStruct.new( :slogan => header.slogan, :image => header.image) end class HeaderStruct < ActionWebService::Struct member :slogan, :string member :image, HeaderImage end ... trying to invoke this gave this SOAP error: SOAP::Mapping::MappingError in Api/categories#invoke_submit Cannot map HeaderStruct to SOAP/OM. So I simplified the struct even more: def header(id) header = Header.find_by_category_id(id) HeaderStruct.new( :slogan => header.slogan, :image => header.image.uri, :transition_image => header.image.transition_uri) end class HeaderStruct < ActionWebService::Struct member :slogan, :string member :image, :string member :transition_image, :string end ... this works as expected; the only additional problem I had was that in development mode on Webrick I had to restart the server for each API change or the changes would not be visible. Hope this helps someone else! SIMEN BREKKEN / born to synthesize.