If I attempt to fetch a record from the db like:
如果我试图从db中获取一条记录,比如:
@user = User.find_by_user_name("blah")
How do I check if the value was null? i.e. the record was found?
如何检查该值是否为空?也就是说找到了记录?
And should I use and
or &&
as they seem to be different?
我是否应该使用或者&&因为它们看起来是不同的?
if @user and @user.age > 0
....
end
5 个解决方案
#1
1
if @user
# not nil
else
# nil
end
should be suffice to check if the value was not nil.
应该足以检查值是否为nil。
And the difference between && and 'and' is that && has a higher precedence over 'and'. See here
与“And”的区别在于&&的优先级高于“And”。在这里看到的
#2
0
This will raise an exception if the value is null:
如果值为空,则会引发异常:
@user = User.find_by_user_name!("blah")
#3
0
If there's no record, @user will be nil, so an if check will work.
如果没有记录,@user将为nil,因此,如果检查将工作。
The difference between && and and is precedence. && has higher precedence.
与&&之间的区别是优先级。& &具有更高的优先级。
#4
0
@user will be nil if it hasn't been found, as suggested. You can check for nil explicitly like so, of course:
@user如果没有找到,将为nil。你可以像这样明确地检查nil,当然
@user.nil?
@user === nil
nil will falsify an if statement, like false. See NilClass and FalseClass for differences.
nil将伪造if语句,比如false。差异见NilClass和FalseClass。
#5
0
As mentioned by others, the finder will return nil
if the record was not found, thus you have a few options, including the one you mentioned in your question and the other answers here.
正如其他人所提到的,如果没有找到记录,finder将返回nil,因此您有一些选项,包括您在问题中提到的那个选项和这里的其他答案。
Since @user = User.find_by_user_name("blah")
evaluates to nil
if the user isn't found, I personally prefer the following Ruby idiom:
由于@user = User.find_by_user_name(“blah”)在未找到用户时将计算为nil,所以我个人更喜欢下面的Ruby习惯用法:
if @user = User.find_by_user_name("blah")
# do stuff with @user
else
# not found
end
#1
1
if @user
# not nil
else
# nil
end
should be suffice to check if the value was not nil.
应该足以检查值是否为nil。
And the difference between && and 'and' is that && has a higher precedence over 'and'. See here
与“And”的区别在于&&的优先级高于“And”。在这里看到的
#2
0
This will raise an exception if the value is null:
如果值为空,则会引发异常:
@user = User.find_by_user_name!("blah")
#3
0
If there's no record, @user will be nil, so an if check will work.
如果没有记录,@user将为nil,因此,如果检查将工作。
The difference between && and and is precedence. && has higher precedence.
与&&之间的区别是优先级。& &具有更高的优先级。
#4
0
@user will be nil if it hasn't been found, as suggested. You can check for nil explicitly like so, of course:
@user如果没有找到,将为nil。你可以像这样明确地检查nil,当然
@user.nil?
@user === nil
nil will falsify an if statement, like false. See NilClass and FalseClass for differences.
nil将伪造if语句,比如false。差异见NilClass和FalseClass。
#5
0
As mentioned by others, the finder will return nil
if the record was not found, thus you have a few options, including the one you mentioned in your question and the other answers here.
正如其他人所提到的,如果没有找到记录,finder将返回nil,因此您有一些选项,包括您在问题中提到的那个选项和这里的其他答案。
Since @user = User.find_by_user_name("blah")
evaluates to nil
if the user isn't found, I personally prefer the following Ruby idiom:
由于@user = User.find_by_user_name(“blah”)在未找到用户时将计算为nil,所以我个人更喜欢下面的Ruby习惯用法:
if @user = User.find_by_user_name("blah")
# do stuff with @user
else
# not found
end