Reputation: 1
All I am trying to do is save a two-dimensional hash in the column of a database in a Ruby on 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.
@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
Upvotes: 0
Views: 723
Reputation: 16564
Get rid of the ":" in front of serialize:
class Model < ActiveRecord::Base
serialize :hash_field
end
Upvotes: 0
Reputation: 599
Hash.new { |h, k| h[k] = Hash.new { |h1, k1| h1[k1] =0 }} => {}
Upvotes: 1