Skip to content
This repository

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

Closed
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">
<project>
<id>58</id>
<name>2G_read_out_Engine</name>
<identifier>dsre</identifier>
<description/>
<created_on>2011-02-08T16:23:56+01:00</created_on>
<updated_on>2011-06-21T13:57:01+02:00</updated_on>
</project>
<project>
<id>55</id>
<name>Automatic TM charts</name>
<identifier>atmc</identifier>
<description/>
<created_on>2011-01-06T13:04:46+01:00</created_on>
<updated_on>2011-01-06T13:04:46+01:00</updated_on>
</project>
<project>
</projects>

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) }
end

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) }
end
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
        val.collect!{|subval|
          block.call subval
        }
        ret = val
      end
    }
    return ret
  end
end

and remember its an evil hack

Jean-Philippe Boily

Thanks for your evil hack @erikkarllen!

Arun Agrawal
Collaborator

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)
    ActiveResource::Formats::XmlFormat.decode(xml)['projects']
  end
end

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

Thanks that is indeed a better solution

Erik Kallen erikkallen closed this January 19, 2012
rilian

thanks!

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.