开发者

MyModel.new.from_json giving no data in Rails 3.0.3

开发者 https://www.devze.com 2023-02-09 11:27 出处:网络
I have some Rails 2.3.x code that I would like to run on Rails 3.0.3, but it seems to fail in my JSON to ActiveRecord code. Her开发者_StackOverflow中文版e is what I do:

I have some Rails 2.3.x code that I would like to run on Rails 3.0.3, but it seems to fail in my JSON to ActiveRecord code. Her开发者_StackOverflow中文版e is what I do:

mymodel = MyModel.new.from_json(json_string)

I get no errors here, but my object is empty as if there was no data in my json_string, but there is. If I look at the API (http://apidock.com/rails/ActiveRecord/Serialization/from_json) then it looks to me as it is not supported in 3.x, or?

I have tried with ActiveSupport::JSON.decode, but that decodes into a hashtable of key/values it seems?


Same problem here. The culprit is https://github.com/rails/rails/commit/c1d73270717f30498f8f4d55d6695509107c2834, here is an explanation: http://www.simonecarletti.com/blog/2010/04/inside-ruby-on-rails-serializing-ruby-objects-with-json/

The solution seems indeed to use

   @obj.attributes = ActiveSupport::JSON.decode(json)

instead of "@obj.from_json(json)". At least for ActiveRecord objects, this works for me.

0

精彩评论

暂无评论...
验证码 换一张
取 消