本文是小编为大家收集整理的关于ruby sequel gem-how to query arrays with pg_array extension的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
我正在使用和续集版本4.1.1.
我添加了这样的扩展名:
Sequel::Database.extension :pg_array
我创建了这样的列:
alter_table :emails do add_column :references, "text[]", null: true end
我可以将数组加载到后阵列列中,就像使用普通数组一样.
从上面的链接中不清楚的是如何根据此数组列中的值执行查询.
例如,如果电子邮件表中的一行包含这些值,请参考列中:
references -------------------------------------------------------------------- {} {5363f773bccf9_32123fe75c45e6f090953@Pauls-MacBook-Pro.local.mail}
我如何查询电子邮件表以查找包含上述值的参考数组值的行:
Email.where(references: ????)
推荐答案
使用pg_array_ops扩展:
Sequel.extension :pg_array_ops Email.where(Sequel.pg_array_op(:references).contains('5363f773bccf9_32123fe75c45e6f090953@Pauls-MacBook-Pro.local.mail'))
其他推荐答案
您是否尝试过?
ref = '5363f773bccf9' emails = Email.arel_table Email.where( emails[ :references ].matches( "%#{ref}%" ))
问题描述
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: ????)
推荐答案
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'))
其他推荐答案
Have you tried?
ref = '5363f773bccf9' emails = Email.arel_table Email.where( emails[ :references ].matches( "%#{ref}%" ))