お疲れ様です。
Ruby on RailsでarrayをHeroku本番環境に入れようとしたのですが
下記のエラーで失敗してしまいます。
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.
ソースコード
migrationfile
Ruby
1class AddDaysToSchedule < ActiveRecord::Migration[5.2] 2 def change 3 add_column :schedules, :days, :text, array: true 4 end 5end
schedule.rb
Ruby
1class Schedule < ApplicationRecord 2 serialize :days, Array 3・・・
試したこと
Postgresqlでは、どうやらserializeを使ってはいけないということは分かって
schdule.rbを下記のserializeを外してみると実際にビルド自体は成功したのですが
出力する際に、arrayと認識してくれずstringとなってしまうため出力でエラーとなってしまいます。
追記
String型に保存して、その後配列に出力することを試みました。
migrationfile
Ruby
1class AddDaysToSchedule < ActiveRecord::Migration[5.2] 2 def change 3 add_column :schedules, :days, :string 4 end 5end
daysを出力するコード
[28] pry(#<EventType>)> self.days => "[0, 1, 2, 3, 4, 5, 6]" [29] pry(#<EventType>)> self.days.split('') => ["[", "0", ",", " ", "1", ",", " ", "2", ",", " ", "3", ",", " ", "4", ",", " ", "5", ",", " ", "6", [30] pry(#<EventType>)> self.days.split => ["[0,", "1,", "2,", "3,", "4,", "5,", "6]"]
このやり方は取得したいデータではないのですが、
文字列で保存して、配列をシリアライズ化する最適な方法はどんなやり方がございますでしょうか。
すみませんが、どうぞよろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/15 12:21
2018/10/15 12:57
2018/10/16 07:02
2018/10/16 07:38