使用 OR(而不是 AND)组合两个命名范围
我想找到所有主体为以下内容的Annotations
:
- 等于“?”
- 或
- 像“[?]”
最好的方法是什么?
如果可能的话,我想使用 SearchLogic ,但尽管 SearchLogic 允许您执行以下各项操作:
Annotation.body_equals('?')
Annotation.body_like('[?]')
并且您始终可以将它们链接在一起:Annotation.body_equals('?' ).body_like('[?]')
我不知道如何将它们与 OR
结合起来。
请注意,您可以组合命名范围与 OR
if 他们的论点是相同的。 例如,我可以这样做:
Annotation.body_equals_or_body_like('?')
但这没有帮助。
请注意,我不喜欢 SearchLogic,但对于不需要破坏其抽象的解决方案来说,这将是很棒的。
I want to find all Annotations
whose bodies are either:
- Equal to "?"
- or
- Like "[?]"
What's the best way to do this?
I would like to use SearchLogic if possible, but though SearchLogic allows you to do each of the following:
Annotation.body_equals('?')
Annotation.body_like('[?]')
and you can always chain them together: Annotation.body_equals('?').body_like('[?]')
I'm not sure how to combine them with OR
.
Note that you can combine named scopes with OR
if their argument is the same. E.g., I could do:
Annotation.body_equals_or_body_like('?')
But this wouldn't help.
Note that I'm not attached to SearchLogic, but it would be great for a solution that doesn't require breaking its abstraction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
“喜欢”结果不也包括“等于”结果吗?
您还可以在另一个命名范围的末尾使用一个命名范围来创建一个非常长的命名范围。来自 Searchlogic 文档(这种方式对我来说似乎有点冗长):
或者您可以使用联合来组合搜索结果数组,同时删除重复项:
Wouldn't the 'like' results also include the 'equals' results?
You can also use a named scope on the end of another to make a really long named scope. From the Searchlogic Docs (this way seems a bit longwinded to me):
Or you can use a union to combine the search result arrays while removing the duplicates:
对于 Rails 2.x,您可以使用以下命名范围来模拟 OR:
让我们通过上面的示例来使用此函数。
上面的代码只执行一个查询。
q1
和q2
不保存查询结果,它们的类是ActiveRecord::NamedScope::Scope
。or
named_scope 组合这些查询并使用 OR 连接条件。您还可以嵌套 OR,就像在这个人为的示例中一样:
因为
or
返回一个ActiveRecord::NamedScope::Scope
本身,我们可能会变得非常疯狂:我相信大多数尽管我还没有尝试过,但这些示例在 Rails 3 中使用
scope
可以正常工作。一点无耻的自我推销 - fake_arel gem 中提供了此功能。
For Rails 2.x, you could use the following named scope to simulate OR:
Let's use this function using your example above.
The above code executes only one query.
q1
andq2
do not hold the results of the query, rather, their class isActiveRecord::NamedScope::Scope
.The
or
named_scope combines these queries and joins the conditions with an OR.You could also nest ORs, like in this contrived example:
Because
or
returns aActiveRecord::NamedScope::Scope
itself, we can go really crazy:I believe that most of these examples would work fine using
scope
s in Rails 3, although I have not tried.A Bit of shameless self-promotion - This functionality is available in the fake_arel gem.
我在这个问题上寻找“或”两个named_scopes的答案,所有的答案对我来说看起来都太复杂了。我进行了一些研究,找到了一个使用名为“or”的附加命名范围的解决方案,它可以解决问题。
按照给定的示例:
两者都返回一个命名范围对象,该对象构造选择返回注释记录
现在我们定义另一个命名范围,期望两个命名范围作为参数,例如:
然后您可以使用:
这将创建一个查询,例如:
这就是您所追求的
As 或也是一个命名范围,可以与其他命名范围链接,包括另一个或:
I came over this question looking for the answer to "or" two named_scopes and all the answers looked too complex to me. I investigated a bit and found a solution using an additional named_scope called "or" which does the trick.
Following the given example:
both return a named_scope object that construct selects returning annotation records
now we define another named scope expecting two named scopes as parameters like:
You can then use:
This will create a query like:
Which is what you were after
As or is also a named_scope, it is possible to chain with other named_scopes including another or:
大概是
Probably it's
我找不到任何简单的解决方案,但这个问题引起了我的兴趣,所以我推出了自己的解决方案:
考虑以下设置:
您可以使用一系列范围的名称来调用它:
这将返回一个如下的范围:
您可以当作用域需要参数时,也可以使用数组数组来调用它:
在您的情况下 Horace,您可以使用以下内容:
I couldn't find any simple solutions, but this problem intrigued me, so I rolled my own solution:
Consider the following setup:
You call it with the names of a series of scopes as such:
This returns a scope like this:
You can also call it with an array of arrays when the scope requires parameters:
In your case Horace, you could use the following: