0

我尝试使用arraycolumn 部署到 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

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

4

1 回答 1

1

解决方案 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
于 2018-10-15T06:48:19.477 回答