使用ruby在Mysql中保存哈希并读取为哈希

时间:2013-02-13 11:18:54

标签: ruby-on-rails

我的代码就是这个

   leads_hash={"509351475764673"=>{"sc"=>0, "ct"=>#<OrderedHash {"2013-02-10"=>1}>, "ml"=>0}, "473882175995429"=>{"sc"=>0, "ct"=>0, "ml"=>0}}
    obj=User.new()
    obj.leads_hash=leads_hash

obj.save!

并在模型i中定义

class User < ActiveRecord::Base
 serialize :leads_hash
end

但它保存为字符串我怎么能解决这个问题请有人帮助我

1 个答案:

答案 0 :(得分:2)

确保在迁移中将leads_hash存储为text而不是string。作为参考,请查看类似的问题:Using Rails serialize to save hash to database