Undefined method `bytesize' for #<Hash>

tknv picture tknv · Jul 14, 2009 · Viewed 20.6k times · Source

I'd like to store and update blogger labels to datastore in GAE. When I run that code, I get this error:

javax.servlet.ServletContext log: Application Error
/base/data/home/apps/yet-another-problem/1.334886515480009498/WEB-INF/gems/gems/sinatra-0.9.2/lib/sinatra/base.rb:45:in `each': undefined method `bytesize' for #<Hash:0x86684c> (NoMethodError)

The Code

class Labels
   class LabelData
    include Bumble
    ds :blog_element_labels
   end

  def update
    response = URLFetch.get($label_url)
    result = response.to_s
    result_headless = result.gsub("listLabels(",'')
    pure_result = result_headless.gsub(");",'')
    json_to_yaml = YAML::load(pure_result)['entry']['category']

    json_to_yaml.each do |label|
    @label = LabelData.find(:blog_element_labels => label['term'])
    @label = LabelData.create(:blog_element_labels => label['term']) if @label.nil?
    end
  end
end

and run by cron job does '/job'

get '/job' do
  @labels = Labels.new
  @labels.update
end

Where is the problem? Please teach me.

But when run cron job first time, label data was stored, even occur that error. Could not update data.

Answer

Luke Antins picture Luke Antins · Jul 14, 2009

I think your having the same problem that's been discussed here: error happens when I try "all" method in datamapper

In your case, Sinatra is trying to take the return value of @lavels.update and turn that into a string to display to the user.

Try this to see if it fixes the problem:

get '/job' do
  @labels = Labels.new
  @labels.update
  "Labels Updated"
end

Your return value is now a string, so you shouldn't get the error.