Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Rails 2.1.2 from_json broken?

#1544

I can't call the from_json method on any ActiveRecord object, no matter what the passed string is. I tested this on windows and on linux with Rails 2.1.2 and it failed, so I think it's probably broken.. Please provide a workaround.

$ script/generate model Blah title:string description:text $ rake db:migrate

blah = Blah.new => #blah.from_json('asdf') NoMethodError: undefined method stringify_keys!' for [asdf](String)

from /var/lib/gems/1.8/gems/activerecord-2.1.2/lib/active_record/base.rb:2366:in `attributes='
from /var/lib/gems/1.8/gems/activerecord-2.1.2/lib/active_record/serializers/json_serializer.rb:64:in `from_json'
from (irb):3

Reported by jc-denton · December 10th, 2008 @ 11:16 AM

State: invalid
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Frederick Cheung
    Frederick Cheung

    You have to pass a json string that corresponds to a hash - what would blah.from_json('asdf') even mean?

    
    Blah.new.from_json("{title: 'hello world'}")
    

    works fine for me.

    December 10th, 2008 @ 12:23 PM

  2. jc-denton
    jc-denton

    yes it does, thank you. However I don't understand why I get a no method error and not "invalid json string" or something like this

    December 10th, 2008 @ 12:29 PM

  3. Frederick Cheung
    Frederick Cheung

    That's probably a detail of the JSON parser in rails - if you just give it a string it returns that string whereas if you pass a string in the correct form it returns a hash (which of course causes problem with the code that assumes it has a hash)

    December 10th, 2008 @ 12:53 PM

  4. Frederick Cheung
    Frederick Cheung
    • State changed from new to invalid

    December 10th, 2008 @ 12:53 PM

  5. jc-denton
    jc-denton

    does invalid mean closed? It's ok to close it, however I'm unhappy with the error message above..

    December 10th, 2008 @ 04:07 PM

  6. Frederick Cheung
    Frederick Cheung

    Yes basically. The core issue "does from_json work?" has been addressed. Nicer failure modes would be nice. Please do investigate if you are concerned.

    December 10th, 2008 @ 04:15 PM