Ruby-on-rails Rails 中的多个 where 条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15060479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:28:08  来源:igfitidea点击:

Multiple where conditions in Rails

ruby-on-railsrubyactiverecordrails-activerecord

提问by X?pplI'-I0llwlg'I -

I'm implementing a user search feature in my Rails app. However, I don't want admins to appear in the search results.

我正在我的 Rails 应用程序中实现用户搜索功能。但是,我不希望管理员出现在搜索结果中。

I'm trying this:

我正在尝试这个:

User.where(:admin => [nil, false], ["name LIKE ?", "%#{params[:query]}%"])

But I get this error:

但我收到此错误:

syntax error, unexpected ')', expecting tASSOC

So how do I properly list whereclauses inside the parentheses?

那么如何正确地列出where括号内的子句呢?

回答by Ismael Abreu

Try this

尝试这个

User.where(["name LIKE ?", "%#{params[:query]}%"]).where(:admin => [nil, false])