Search code examples
ruby-on-railsruby-on-rails-3activeresource

Why is ActiveResource returning Arrays of Hashes instead of objects?


I've just started building an api and application that consumes from the api using ActiveResource. I'll get into specifics with an edit, but my initial problem is this:

I have a controller that does Resource.find(resource.id), that will return an object, and if I include on the api side, the included association are also easily worked with objects. To pull a more customized collection the application goes to a custom action on the api, this will return an Array of Hashes that isn't as easily worked with(e.g. with link_to).

First question: why?

As stated I'll fill in more if necessary (it gets more complicated from my perspective, maybe not so from a professional/expert perspective so I'll hold off for now). I'm hoping someone can give an explanation allowing me more control over ActiveResource.


Solution

  • http://api.rubyonrails.org/classes/ActiveResource/CustomMethods.html

    the custom resource used .get, so:

    " Note: the objects returned from this method are not automatically converted into ActiveResource::Base instances - they are ordinary Hashes. If you are expecting ActiveResource::Base instances, use the find class method with the :from option. "

    So, trying another way....