Ruby-on-rails Rails where 条件为零值

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

Rails where condition with nil value

ruby-on-railsnull

提问by Swe

I found out that using where with symbol :my_id => nil and using old school one with ? is different. Could anyone explain me why?

我发现使用 where 和符号 :my_id => nil 并使用老式的?是不同的。谁能解释我为什么?

MyTable.where("my_id = ? ", nil).first
SELECT `my_tables`.* FROM `my_tables` WHERE (my_id = NULL ) LIMIT 1

Does not get any data

没有得到任何数据

MyTable.where(:my_id => nil).first
SELECT `my_tables`.* FROM `my_tables` WHERE (`my_tables`.`my_id` IS NULL) LIMIT 1

Get data which has my_id is null.

获取 my_id 为空的数据。

What is the best practise to use in rails?

在 Rails 中使用的最佳实践是什么?

I think I didn't make clear about my question. In my rails application, request parameter is nil. Existing coding is MyTable.where(:my_id => params[:id]).first In table, there are lots of records which have my_id is null. Therefore, the first record from table is pick up without realizing. First of all, yes it is the problem with unclean data in table.

我想我没有说清楚我的问题。在我的 rails 应用程序中,请求参数为零。现有的编码是 MyTable.where(:my_id => params[:id]).first 在表中,有很多记录的 my_id 为空。因此,表中的第一条记录被无意识地拾取。首先,是的,这是表中数据不干净的问题。

To solve this problem. I find two solutions

为了解决这个问题。我找到两个解决方案

Solution 1

解决方案1

if params[:id].present?
  MyTable.where(:my_id => params[:id]).first
end

Solution 2

解决方案2

MyTable.where("my_id = ? ", nil).first

As you know, if we put (if condition more and more), our application will get slower and it will not be functional programming. When I try solution 2, I get surprised because I am expecting it should give same result.

如您所知,如果我们放入(如果条件越来越多),我们的应用程序会变慢,并且不会是函数式编程。当我尝试解决方案 2 时,我感到很惊讶,因为我期望它应该给出相同的结果。

回答by Mischa

The correct SQL syntax is my_id IS NULL, so if you change your first snippet to the following it willwork:

正确的 SQL 语法是my_id IS NULL,因此如果您将第一个代码段更改为以下内容,它将起作用:

MyTable.where("my_id IS ?", nil).first

Both syntaxes are perfectly fine. It's up to your own preference. However, if it's a parameter and you don't know whether it will be nilor not, you'd better use:

两种语法都很好。这取决于你自己的喜好。但是,如果它是一个参数并且您不知道它是否会出现nil,则最好使用:

MyTable.where(:my_id => parameter).first

回答by BookOfGreg

In rails 4 you can specify the value as null using the new hash syntax.

在 rails 4 中,您可以使用新的哈希语法将该值指定为 null。

MyTable.where(my_id: nil).first

回答by leenasn

For DB for NULL the syntax should be

对于 NULL 的 DB,语法应该是

my_id IS NULL

So you can give it as:

所以你可以把它作为:

MyTable.where("my_id is NULL ").first

回答by Bohdan

I suppose it's so called "rails magic" you can pass ranges

我想这就是所谓的“rails magic”,你可以传递范围

Client.where(:created_at => (Time.now.midnight - 1.day)..Time.now.midnight)

becomes

变成

SELECT * FROM clients WHERE (clients.created_at BETWEEN '2008-12-21 00:00:00' 
                                                    AND '2008-12-22 00:00:00')

or if you pass a subset

或者如果你传递了一个子集

Client.where(:orders_count => [1,3,5])

rails will do

导轨会做

SELECT * FROM clients WHERE (clients.orders_count IN (1,3,5))

more

更多的