2013-05-04 70 views
0

我有一個belongs_to帳戶的課程模型。如何編寫輪胎查詢其中的association_id爲零或特定值?

公共課程沒有account_id。

私人課程在account_id中有價值。

如何爲公共和私人課程中的某個搜索字詞進行課程搜索?

LessonContentBlock.search(
    :load => true, 
    :page => (params[:page] || 1) 
) do 
    query do 
    boolean do 
     should { string q } 
     filter :missing => { :field => 'account_id' } 
     should { string "account_id:#{a.id}" } 
    end 
    end 
end 

我也試過這樣:

LessonContentBlock.search(
    :load => true, 
    :page => (params[:page] || 1) 
) do 
    query { string q } 
    filter :missing, :field => 'account_id' 
    filter :term, :account_id => a.id 
end 
+0

您的代碼引用了變量'a',該變量似乎是您要過濾的特定*帳戶。這是否意味着您試圖查找屬於這個單個私人帳戶「a」或公開的記錄? – 2013-05-04 22:37:14

回答

1

如果我理解你的問題,那麼你想要做這樣的事情:

LessonContentBlock.search(
    :load => true, 
    :page => (params[:page] || 1) 
    ) do 
    query { string q } 
    filter :or, {:term => {:account_id => a.id}}, 
       {:missing => {:field => 'account_id'}} 
end 

我想我知道如何解決這個,但掙扎着讓它自己工作。最終在Tire的測試套件中找到了一個很好的例子:http://git.io/v0PGBw