保存对象

时间:2017-07-03 16:50:15

标签: ruby-on-rails-3 serialization

ruby 2.3.3rails 3.2.22.5

您好。我在serialized :logs, Hash模型中使用User,在保存日志哈希时 - 之后它将被检索为字符串而不是哈希。

如何发生:

  1. 将值分配给日志哈希
  2. 保存用户对象
  3. 现在日志哈希是一个字符串..
  4. 因此,在为userlogs 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"
    

    有什么想法吗?

1 个答案:

答案 0 :(得分:0)

确定。所以(据我所知)Rails version: 3.2.22.5ruby 2.2.x兼容,但与ruby 2.3.x不兼容

通过将ruby 2.3.3还原为ruby 2.2.6

来修复它