This project is archived and is in readonly mode.
Rails 2.1.2 from_json broken?
Reported by jc-denton | December 10th, 2008 @ 11:16 AM | in 2.x
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
Comments and changes to this ticket
-
Frederick Cheung December 10th, 2008 @ 12:23 PM
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.
-
jc-denton December 10th, 2008 @ 12:29 PM
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
-
Frederick Cheung December 10th, 2008 @ 12:53 PM
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)
-
Frederick Cheung December 10th, 2008 @ 12:53 PM
- State changed from new to invalid
-
jc-denton December 10th, 2008 @ 04:07 PM
does invalid mean closed? It's ok to close it, however I'm unhappy with the error message above..
-
Frederick Cheung December 10th, 2008 @ 04:15 PM
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.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>