PostgreSQL :: OID :: Array不支持`serialize`功能

时间:2018-10-15 06:27:41

标签: ruby-on-rails ruby heroku

我尝试使用array列部署到Heroku。但是会发生此错误。我可以使用什么方法代替serialize更好呢?

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.

//迁移文件

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

//时间表模型

class Schedule < ApplicationRecord
  serialize :days, Array

请让我知道您知道如何堆栈溢出。

1 个答案:

答案 0 :(得分:0)

解决方案1:=>

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

无需序列化

class Schedule < ApplicationRecord
  #serialize :days, Array

解决方案2:=>

我建议您这样:-

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

并将模型中的列序列化为数组

class Schedule < ApplicationRecord
  serialize :days, Array
end

要存储值:-

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

获取数组值

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