8

I am using the pg_array extension and sequel version 4.1.1.

I have added the extension like this:

Sequel::Database.extension :pg_array

I have created a column like this:

alter_table :emails do
  add_column :references, "text[]", null: true
end

I can load and retrieve arrays into a postgress array column, just like working with normal arrays.

What is not clear from the above link is how do I execute a query based on the values in this array column.

For example, if one row in the emails table contained these values in the references column:

                             references                             
--------------------------------------------------------------------
 {}
 {[email protected]}

How can I query the emails table to find a row that contains a references array value of the above value:

Email.where(references: ????)
3
  • Please put the example here in the post, for which you want help ? Commented May 2, 2014 at 19:41
  • @ArupRakshit is that clearer? Commented May 2, 2014 at 19:57
  • Usually you'll want to use operators like @> to determine membership, right? Commented May 2, 2014 at 19:57

2 Answers 2

9

Use the pg_array_ops extension:

Sequel.extension :pg_array_ops
Email.where(Sequel.pg_array_op(:references).contains('[email protected]'))
Sign up to request clarification or add additional context in comments.

4 Comments

Is this answer still up to date?
@SeanDunford, for my sequel#5.25.0 & psql#11.5 it works
what is the correct way to make a schema migration for array fields with index?
Working on Sequel 4, it requires .contains('{[email protected]}')
-1

You can try:

ref = '5363f773bccf9'
emails = Email.arel_table
Email.where( emails[ :references ].matches( "%#{ref}%" ))

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.