ruby 2.3.3
,rails 3.2.22.5
您好。我在serialized :logs, Hash
模型中使用User
,在保存日志哈希时 - 之后它将被检索为字符串而不是哈希。
如何发生:
因此,在为user
:logs hash
分配值时user.logs[2] = 4
,然后保存
class User < ActiveRecord::Base
serialize :logs, Hash # in database it's a text type
end
rails控制台中的一个示例:
# rails console
test_user_id = 3
user = User.find(test_user_id)
user.logs = {}
user.save
user.logs # => {} # great
User.find(test_user_id).logs # => {} # great
# now trying to add value and save:
# 1. assign value to logs hash
user.logs[Time.now] = {:field_id=>6} # => {:field_id=>6}
# 2. save user object:
user.save
# 3. now logs hash is a string..
user.logs
#=> "---\n2017-07-03 19:33:34.938364000 +03:00:\n :field_id: 6\n"
# why it turned to string ?!?!
User.find(test_user_id).logs # same here:
# => "---\n2017-07-03 19:33:34.938364000 +03:00:\n :field_id: 6\n"
有什么想法吗?
答案 0 :(得分:0)
确定。所以(据我所知)Rails version: 3.2.22.5
与ruby 2.2.x
兼容,但与ruby 2.3.x
不兼容
通过将ruby 2.3.3
还原为ruby 2.2.6