Skip to content
This repository

ActiveResource undefined method `collect!' for #<Hash:0x0000010367cc90> #2318

erikkallen opened this Issue July 28, 2011 · 11 comments

8 participants

Erik Kallen Gary Moore Jean-Philippe Boily Arun Agrawal Tom Rossi Ryan Marsh Vasily Mikhaylichenko rilian
Erik Kallen

When trying to load a ActiveResouce xml collection I get the following error:

undefined method `collect!' for #Hash:0x0000010367cc90

This is the xml it tries to read:

<projects type="array" offset="0" total_count="55" limit="25">
<name>Automatic TM charts</name>

I traced the error to this method in ActiveResource::Base line 922

def instantiate_collection(collection, prefix_options = {})
collection.collect! { |record| instantiate_record(record, prefix_options) }

if I replace it with this it works:

def instantiate_collection(collection, prefix_options = {})
collection = collection[element_name.pluralize] if collection.instance_of?(Hash)       
collection.collect! { |record| instantiate_record(record, prefix_options) }
Gary Moore

Hey Eric, you ever put in a pull request for this? We're having the same problems here when trying to interpret xml that isn't from us.

Erik Kallen

No sorry I haven't I'll try to make one tomorrow I used an evil hack for now which I can hand you if you want for the time beeing

Gary Moore

Yeah would love to take a look. Send it over.

Erik Kallen

I found the evil hack sorry it took so long

add this to your config/boot.rb:

class Hash
  def collect!(&block)
    ret = []
    self.each {|key,val|
      if val.kind_of? Array
        ret = val
    return ret

and remember its an evil hack

Jean-Philippe Boily

Thanks for your evil hack @erikkarllen!

Arun Agrawal

Just found same bug reported here #1498

Tom Rossi

I'm turning to the darkside and using the evil hack!!!

Ryan Marsh

Using the evil hack.... yuck.

Vasily Mikhaylichenko

Don't do it folks! ActiveResource lets you specify a custom formatter for you data which you can use to expand your hash:

class ProjectXMLFormatter
  include ActiveResource::Formats::XmlFormat

  def decode(xml)

class Project < ActiveResource::Base
  self.format =
Erik Kallen

Thanks that is indeed a better solution

Erik Kallen erikkallen closed this January 19, 2012


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.