2

I have a column set up like this in my table

add_column :logs, :names, :string, array: true
add_index :logs, :names, using: :gin

I have this query to search the table via the names column to return records that contain robert

Logs.where("? = ANY (names)", 'robert')

I am curious on how to search the table for multiple names. Something like this, but the below query does not work

Logs.where("? = ANY (names)", ['robert', 'bob'])

I want it to function to something similar like this, where I pass in an array and returns all records that match

User.where(id: [1,2,3,4])

I need it as an OR statement

0

2 Answers 2

1
User.where(names: ['robert', 'bob'])

User.where("names IN (?) ", ['robert', 'bob'])

note: Its case sensitive

This answer will also helps you

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

5 Comments

I need the statement to be an OR statement so if name contains either robert or bob it should return it
also note the column is names and it is an array
@RobMason I have updated the answer. Can u please check it now..
error message: Array value must start with "{" or dimension information
@RobMason It works fine for me. can u please recheck it.
1
# SQL Contains Operator
1) User.where("names @> ARRAY[?]::varchar[]", ['robert', 'bob'])


# Contains Operator
2) User.where.contains(names: ['robert', 'bob'])


# Overlap Operator
3) User.where.overlap(names: ['robert', 'bob'])

1 Comment

I need the statement to be an OR statement so if names contains either robert or bob it should return it

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.