0

I created a new migration for my Rails app on Heroku. It adds some Array columns like this:

t.string :timezone, array: true, default: [].to_yaml
t.string :locale, array: true, default: [].to_yaml

I get the following error when I try to migrate:

ActiveRecord::StatementInvalid: PG::InvalidTextRepresentation: ERROR: malformed array literal: "--- [] " DETAIL: Array value must start with "{" or dimension information. : CREATE TABLE "filters" ("id" serial primary key, "letter_id" integer, "gender" character varying, "timezone" character varying[] DEFAULT '--- [] ', "locale" character varying[] DEFAULT '--- []

Here is my model:

class Filter < ApplicationRecord
  belongs_to :letter
  serialize :timezone
  serialize :locale
  serialize :segment
  validates_uniqueness_of :letter_id
end

Some people on Stackoverflow say removing serialize will do the trick but I need to store an array, not a string.

Any idea how I can solve this issue?

2 Answers 2

0

Why don't you just do:

t.text :your_table, :timezone, default: []
t.text :your_table, :locale, default: []

Not sure why you would ever call to_yaml for a default value. Also, you should use a column type of text since string will likely limit you to 255 characters by default.

Sign up to request clarification or add additional context in comments.

Comments

0

In the end, I did the following and it solved my issue:

t.string :timezone, :string
t.string :locale, :string

And kept my model with serialize.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.