开发者

serialized column always saved as null in Rails

All I am trying to do is save a two-dimensional hash in the column of a database in a Ruby on开发者_如何学Python Rails application. I am only learning the how to use the framework, and this task is causing me a lot of grief. I have done my best to not make any stupid mistakes, though I believe my problem stems from one.

So what I'm doing is I create the hash in a controller and set it as the value of a field in my object, then call save.

Code:

@instance = Model.find(:first, :conditions => "id = 1"}

@instance.hash_field = Hash.new { |h, k| h[k] = Hash.new { |h1, k1| h1[k1] =0 }} 

@instance.other_field = "some string"

@instance.save

other_field will have its new value saved and the value persists on subsequent retrievals from the database. The 'hash_field' will always be reset to nil when I pull the instance out of the database again. This happens despite the fact that the value of '@instance.hash_field' has the correct value in the controller and the view. This allows me to be confident in saying it is an issue with saving the hash to the database.

I have the statement

:serialize :hash_field

in my model, and its column in the database table is declared to have type 'text.' Any pointers would make me a very happy person.

-Chris


  1. 'serialize' is a method.
  2. Hash.new { |h, k| h[k] = Hash.new { |h1, k1| h1[k1] =0 }} => {}
  3. Please provide test data you use for hashes.


Get rid of the ":" in front of serialize:

class Model < ActiveRecord::Base
  serialize :hash_field

end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜