Active resource error expecting hash

880 views
Skip to first unread message

Peter

unread,
May 19, 2015, 4:16:27 AM5/19/15
to rubyonra...@googlegroups.com
Hello,

I want to call a REST API with active resource, described here:
http://www.redmine.org/projects/redmine/wiki/Rest_api_with_ruby

this is my code:
require 'active_resource'

class Issue < ActiveResource::Base
  ActiveResource::Base.include_root_in_json = true
  self.site = 'http://demo.redmine.org/'
end

# Retrieving issues
issue = Issue.find(:all)

and getting this error:
/activeresource-4.0.0/lib/active_resource/base.rb:1327:in `load': expected an attributes Hash, got ["issues", [{"id"=>91663, "project"=>{"id"=>77006, "name"=>"proj1"},....

The link mentions:
You may need to set include_root_in_json = true in your ActiveResource class

But I don't understand where and how.
Greatly appreciate your help.

Cheers,
Peter

Hassan Schroeder

unread,
May 19, 2015, 9:08:40 AM5/19/15
to rubyonrails-talk
On Mon, May 18, 2015 at 2:05 PM, Peter <peter...@gmail.com> wrote:

> I want to call a REST API with active resource, described here:
> http://www.redmine.org/projects/redmine/wiki/Rest_api_with_ruby

> issue = Issue.find(:all)

> and getting this error:
> expected an attributes Hash, got ["issues", [{"id"=>91663, "project"=>{"id"=>77006,
> "name"=>"proj1"},....

Your `find` is expecting a singular element name back: 'issue' rather
than 'issues'.

Take a look at this:
http://www.rubydoc.info/gems/activeresource/4.0.0/ActiveResource/Collection

And do something like this:

** issue.rb **
require 'active_resource'

class Issue < ActiveResource::Base
self.site = 'http://demo.redmine.org/'
self.collection_parser = IssueCollection
end

** issue_collection.rb **
class IssueCollection < ActiveResource::Collection
def initialize(parsed = {})
@elements = parsed['issues']
end
end

HTH,
--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
Consulting Availability : Silicon Valley or remote
Reply all
Reply to author
Forward
0 new messages