0

I'm trying to build a scope for my Contradiction model where the evaluation_parameters attribute only contains non NULL values. I have records where this attribute is completely empty {} or where one of the two values is nil {123, NULL} and I would like to not have these in my result.

Here is my migration:

add_column :contradictions, :evaluation_parameters, :integer, array: true, default: []

I already tried the following query which returns only records with an empty array

Contradiction.all.where.not("NULL = ANY (evaluation_parameters)")

Any suggestions on how I could do this ? Thank you :)

2 Answers 2

0

First of all:

evaluation_parameters should probably be :text, not :integer.

The query you're looking for then is:

Contradiction.where("evaluation_parameters = '{}'")

This would fetch all contradictions which have evaluation_parameters equal to empty array.

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

1 Comment

Thank you for your input, however doing this only gives me the results with an empty array. What I want is all contradictions where both values in the array are NOT null (e.g. {123, 123}). I also don't see why I would have to convert to :text since I'm storing ids in this array.
0

I found an answer here Check if NULL exists in Postgres array My final query looks like this :

Contradiction.where.not(evaluation_parameters: '{}').where.not("-1 = ANY(evaluation_parameters) IS NULL")

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.