2014-05-02 19 views
4

我使用pg_array擴展和續集版本4.1.1。ruby​​ sequel gem - 如何用pg_array擴展查詢數組

我已經添加了擴展這樣的:

Sequel::Database.extension :pg_array 

我創建這樣的柱:

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

我可以加載和檢索數組到一個postgress陣列列,就像有工作正常的數組。

從以上鍊接中不清楚的是,如何根據此數組列中的值執行查詢。

例如,如果在電子郵件表中的一行在參考文獻中列載這些值:

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

我怎樣才能查詢郵件表來查找包含上述值的引用數組值的行:

Email.where(references: ????) 
+0

請把這裏的例子在後,您要幫忙嗎? –

+0

@ArupRakshit更清晰嗎? – dagda1

+0

200%明確.. :) –

回答

2

使用pg_array_ops擴展:

Sequel.extension :pg_array_ops 
Email.where(Sequel.pg_array_op(:references).contains('[email protected]')) 
0

你試過了嗎?

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