2013-05-04 68 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