2

Fetching collections of books using activeresource.

I have a class extending from active resource as follows:

class BookResource < ActiveResource::Base
  self.site = "http://api.book.com:3000/"
end

Here is my books_controller.rb

class BookController < ApplicationController
  def index
    @book = Book.find(:all)
  end
end

Running the following command in rails console:

2.1.2 :009 >   BookResource::Book.find(:all)

Actually using the above call, it is giving an error called

" ArgumentError: expected an attributes Hash, got ["total_entries", 38]"

2.1.2 :009 >   BookResource::Book.find(:all)
I, [2015-02-25T14:04:00.492054 #12622]  INFO -- : GET http://localhost:3000/api/v2/books.json
I, [2015-02-25T14:04:00.492197 #12622]  INFO -- : --> 200 OK 5489 (50.0ms)
ArgumentError: expected an attributes Hash, got ["total_entries", 38]
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/activeresource-4.0.0/lib/active_resource/base.rb:1327:in `load'
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/activeresource-4.0.0/lib/active_resource/base.rb:1074:in `initialize'
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/activeresource-4.0.0/lib/active_resource/base.rb:1001:in `new'
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/activeresource-4.0.0/lib/active_resource/base.rb:1001:in `instantiate_record'
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/activeresource-4.0.0/lib/active_resource/base.rb:997:in `block in instantiate_collection'
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/activeresource-4.0.0/lib/active_resource/collection.rb:67:in `block in collect!'
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/activeresource-4.0.0/lib/active_resource/collection.rb:7:in `each'
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/activeresource-4.0.0/lib/active_resource/collection.rb:7:in `each'
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/activeresource-4.0.0/lib/active_resource/collection.rb:67:in `collect!'
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/activeresource-4.0.0/lib/active_resource/base.rb:997:in `instantiate_collection'
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/activeresource-4.0.0/lib/active_resource/base.rb:966:in `find_every'
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/activeresource-4.0.0/lib/active_resource/base.rb:873:in `find'
    from (irb):9
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/railties-4.0.4/lib/rails/commands/console.rb:90:in `start'
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/railties-4.0.4/lib/rails/commands/console.rb:9:in `start'
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/railties-4.0.4/lib/rails/commands.rb:62:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'2.1.2 :010 > 
diya
  • 6,938
  • 9
  • 39
  • 55

0 Answers0