首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何编写association_id为空或特定值的轮胎查询?

如何编写association_id为空或特定值的轮胎查询?
EN

Stack Overflow用户
提问于 2013-05-05 03:12:39
回答 1查看 87关注 0票数 0

我有一个belongs_to账号的课程模型。

公共课程的account_id为零。

私人课程在account_id中有价值。

如何为出现在公共课程和私人课程中的某些搜索词编写搜索课程?

代码语言:javascript
复制
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

我也尝试过这个:

代码语言:javascript
复制
LessonContentBlock.search(
  :load => true, 
  :page => (params[:page] || 1)
  ) do
  query { string q }
  filter :missing, :field => 'account_id'
  filter :term, :account_id => a.id
end
EN

回答 1

Stack Overflow用户

发布于 2013-05-05 07:27:34

如果我理解你的问题,那么你应该这样做:

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16377976

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档