Shun Yamada
Shun Yamada

Reputation: 979

PostgreSQL::OID::Array does not support `serialize` feature

I've tried to deploy to Heroku with array column. But this error occurs. And what can I use method instead of serialize is better?

ActiveRecord::AttributeMethods::Serialization::ColumnNotSerializableError (Column `days` of type ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array does not support `serialize` feature.
Usually it means that you are trying to use `serialize`
on a column that already implements serialization natively.

// migration file

class AddDaysToSchedule < ActiveRecord::Migration[5.2]
  def change
    add_column :event_types, :days, :text, array: true
  end
end

// schedule model

class Schedule < ApplicationRecord
  serialize :days, Array

Please let me know you knoe how to stack overflow.

Upvotes: 0

Views: 1801

Answers (2)

septerr
septerr

Reputation: 6593

For those trying to use, say Sqlite in development environment, but Postgres in production, you can serialize conditionally based on the database type. Example:

serialize :days, Array if ActiveRecord::Base.connection.adapter_name == 'SQLite'

Upvotes: 0

Anand
Anand

Reputation: 6531

Solution 1 :=>

class AddDaysToSchedule < ActiveRecord::Migration[5.2]
  def change
    add_column :event_types, :days, :string, array: true, default: []
  end
end

No need to serialize

class Schedule < ApplicationRecord
  #serialize :days, Array

Solution 2:=>

I would suggest you to go like this: -

class AddDaysToSchedule < ActiveRecord::Migration[5.2]
  def change
    add_column :event_types, :days, :string
  end
end

And serialize column in model as array

class Schedule < ApplicationRecord
  serialize :days, Array
end

To store values:-

sh = Schedule.new()
sh.days.push(day_value)
sh.save

To get array value

 sh = Schedule.find(x)
 sh.days => will return array of days

Upvotes: 2

Related Questions