Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby sequel gem - how to query arrays with the pg_array extension

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: ????)
like image 514
dagda1 Avatar asked May 02 '14 19:05

dagda1


1 Answers

Use the pg_array_ops extension:

Sequel.extension :pg_array_ops
Email.where(Sequel.pg_array_op(:references).contains('[email protected]'))
like image 187
Jeremy Evans Avatar answered Nov 11 '22 23:11

Jeremy Evans