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

烈酒焚心 提交于 2019-12-07 03:14:56

问题


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                             
--------------------------------------------------------------------
 {}
 {5363f773bccf9_32123fe75c45e6f090953@Pauls-MacBook-Pro.local.mail}

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

Email.where(references: ????)

回答1:


Use the pg_array_ops extension:

Sequel.extension :pg_array_ops
Email.where(Sequel.pg_array_op(:references).contains('5363f773bccf9_32123fe75c45e6f090953@Pauls-MacBook-Pro.local.mail'))



回答2:


Have you tried?

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


来源:https://stackoverflow.com/questions/23435745/ruby-sequel-gem-how-to-query-arrays-with-the-pg-array-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!