0

A user model has a goal field that is an array:

t.integer "goal", default: [], array: true

A valid entry for goal is [[],[],[]]. If I do

Match.create(goal: [[],[],[]])

I get an exception:

ActiveRecord::StatementInvalid: PG::InvalidTextRepresentation: ERROR:  malformed array literal: "{{},{},{}}"
DETAIL:  Unexpected "}" character.

I know that I can work around this by saving it as goal: [], but I lose information about the number of sub-arrays. Is there a way to fix this?

1 Answer 1

1

You’ve gone from trying to store an array of numbers to an array of fixed length which includes null values or even sub arrays. Therefore, a t.integer field is no good. You need to use a t.jsonb or t.json field.

See the Postgres docs on JSON datatypes

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

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.