Ruby-on-rails Rails ActiveRecord:查找中的多个条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16716787/
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 22:02:13  来源:igfitidea点击:

Rails ActiveRecord: Multiple conditions in find

ruby-on-railsrubysearchactiverecordhashmap

提问by Will Matheson

This might be more of a Ruby syntax thing than anything else. I'm having difficulty getting two limiting conditions on a SomeObject.find going.

这可能更像是一种 Ruby 语法。我很难在 SomeObject.find 上获得两个限制条件。

Separated, the conditions seem to work:

分开,条件似乎有效:

if search != ''
  find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
  find(:all, :conditions => ['active', 1]).shuffle
end

What I'm going for for the first case is this:

我要为第一个案例是这样的:

find(:all, :conditions => ['name LIKE ?', "%#{search}%"], ['active', 1])

But the line throws syntax error, unexpected ')', expecting tASSOC.

但线抛出syntax error, unexpected ')', expecting tASSOC

回答by

Rails 2

导轨 2

find(:all, :conditions => ['name LIKE ?', "%#{search}%"], ['active', 1])isn't the proper syntax for passing hashes to a method. You can leave the curly braces off of a hash if it is the last argument to a method, but in this case you are passing an array as the last argument.

find(:all, :conditions => ['name LIKE ?', "%#{search}%"], ['active', 1])不是将哈希传递给方法的正确语法。如果它是方法的最后一个参数,您可以将花括号从散列中去掉,但在这种情况下,您将传递一个数组作为最后一个参数。

Use the following instead:

请改用以下内容:

find(:all, :conditions => ["name LIKE ? AND active = ?", "%#{search}%", 1])

or

或者

params = {:search => "%#{search}%", :active => 1}
find(:all, :conditions => ["name LIKE :search AND active = :active", params])

Rails 3 and 4

轨道 3 和 4

You would probably want to do something more like the following for recent Rails versions:

对于最近的 Rails 版本,您可能希望执行以下操作:

 scope :active, -> { where(active: true) }
 scope :name_like, ->(search) { where("name LIKE ?", "%#{search}%") }

And then you'd call it like this:

然后你会这样称呼它:

YourModel.active.name_like("Bunnies")

That allows you to reuse those specific queries in different combinations throughout the application. It also makes the code retrieving the data super easy to read.

这允许您在整个应用程序中以不同的组合重用这些特定查询。它还使检索数据的代码非常容易阅读。

If you don't like the scopesyntax, you can also define these as class methods:

如果您不喜欢这种scope语法,也可以将这些定义为类方法:

def self.active
  where(active: true)
end

def self.name_like(search)
  where("name LIKE ?", "%#{search}%")
end

You can also chain scopes on the fly. That will allow you to start building a chain of relation objects and then choose to include others based on conditions. Here's what it could look like when applied to the original question to achieve the same results:

您还可以动态链接范围。这将允许您开始构建关系对象链,然后根据条件选择包括其他对象。以下是应用于原始问题以获得相同结果时的样子:

results = active
results = results.name_like(search) if search.present?

回答by PinnyM

Instead of using if-elsewhich would involve a redundancy for checking on active = 1, a simpler syntax would be something like this:

而不是使用if-elsewhich 会涉及检查 on 的冗余active = 1,更简单的语法是这样的:

result = where(:active => 1)
result = result.where('name like ?', "%#{search}%") unless search.blank?

As far as the error your seeing, it wouldn't appear to be caused by the code that you are posting. A stack trace may help narrow it down further...

至于您看到的错误,它似乎不是由您发布的代码引起的。堆栈跟踪可能有助于进一步缩小范围...

回答by Sachin Singh

i think you are using rails 2

我想你正在使用 rails 2

try this.

尝试这个。

find(:all, :conditions => ['name LIKE ? and active = 1', "%#{search}%"])

rails 3 syntax

rails 3 语法

where('name LIKE ? and active = 1', "%#{search}%")

回答by Deepak Kabbur

For Rails 4, In rails new find_by and find_by! methods are introduced

对于 Rails 4,在 rails 中新增 find_by 和 find_by!方法介绍

Read answer

阅读答案