How to add to a serialized array

后端 未结 3 2026
名媛妹妹
名媛妹妹 2020-12-13 18:18

I have an existing user which has a serialized field and I want to be able to add recent messages to the array / serialized field.

class User < ActiveReco         


        
3条回答
  •  庸人自扰
    2020-12-13 18:45

    You can pass a class to serialize:

    class User < ActiveRecord::Base
      serialize :recent_messages, Array
    end
    

    The above ensures that recent_messages is an Array:

    User.new
    #=> #
    

    Note that you might have to convert existing fields if the types don't match.

提交回复
热议问题