Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Closed
erikkallen opened this Issue · 11 comments

8 participants

@erikkallen

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
@gmoore

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.

@erikkallen

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

@gmoore

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

@erikkallen

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

@jipiboily

Thanks for your evil hack @erikkarllen!

@arunagw
Collaborator

Just found same bug reported here #1498

@tomrossi7

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

@ryanwmarsh

Using the evil hack.... yuck.

@vaskas

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  
@erikkallen

Thanks that is indeed a better solution

@erikkallen erikkallen closed this
@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.