How to add to a serialized array

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
#=> #<User id: nil, recent_messages: [], created_at: nil, updated_at: nil>

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)