简体   繁体   English

Rails:searchlogic搜索或条件

[英]Rails: searchlogic search with or conditions

I'm using the 'binarylogic-searchlogic' gem in version 2.3.5 along with Rails 2.3.4. 我在版本2.3.5中使用'binarylogic-searchlogic'gem以及Rails 2.3.4。

What I want to do is performing a search on a model for a specified value over multiple attributes. 我想要做的是在模型上搜索多个属性上的指定值。 I achieve this through chaining everything together like 我通过将所有东西链接在一起来实现这一点

User.first_name_or_last_name_or_email_like(value)

But with more and more attributes in this search this tends to be ugly. 但是在这个搜索中有越来越多的属性,这往往是丑陋的。 Instead I'd like to use the search mechanism of searchlogic like this: 相反,我想使用searchlogic的搜索机制,如下所示:

search = User.search
search.first_name_like = value
search.last_name_like  = value
..
@users = search.all

So this is the way to search via AND - but what I want is OR. 所以这是通过AND搜索的方式 - 但我想要的是OR。 I've found two ways to achieve this, but both don't work. 我找到了两种方法来实现这一目标,但两种方法都不起作用。

1st one: prepend an or_ to the condition 第一个:在条件前面加上or_

search = User.search
search.first_name_like = value
search.or_last_name_like  = value
@users = search.all

This gives me The or_last_name_like is not a valid condition. You may only use conditions that map to a named scope 这给了我The or_last_name_like is not a valid condition. You may only use conditions that map to a named scope The or_last_name_like is not a valid condition. You may only use conditions that map to a named scope

2nd one: use search.any 第二个:使用search.any

search = User.search
search.first_name_like = value
search.last_name_like  = value
@users = search.any

gives me undefined method any' for #`. 给我一个undefined method 'for #`。

Any idea's on that? 有什么想法吗? Am I mising the right point of the readme? 我是否认为自述的正确点?

Thanks for your very welcome help! 谢谢你非常欢迎的帮助!

edit: time for some ugly workaround: 编辑:一些丑陋的解决方法的时间:

search = User.search
search.first_name_like = value
search.last_name_like  = value
User.find(:all, :conditions => search.scope(:find).gsub('AND','OR'))

Works but is surely not the way to go, isn't it? 工作,但肯定不是要走的路,不是吗?

I don't think there's another way of doing it. 我不认为还有另一种方法。 By default it will join the arguments with AND. 默认情况下,它将使用AND连接参数。

The OR code, only seems to work with chaining. OR代码,似乎只与链接一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM