Ruby-on-rails 当没有记录时 find() 为 nil
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9604585/
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
find() with nil when there are no records
提问by Eqbal
In my current rails program when I use something like
在我当前的 rails 程序中,当我使用类似的东西时
user = User.find(10)
When there is no user with ID=10 , I will have exception like :
当没有 ID=10 的用户时,我会遇到如下异常:
ActiveRecord::RecordNotFound: Couldn't find User with ID=10
Can I get nil instead of raising exception so when I do something like :
我可以得到 nil 而不是引发异常,所以当我做这样的事情时:
unless user = Challenge.find(10)
puts "some error msg"
end
I just want to get nil when there is no records and I don't want to use begin/rescue
我只想在没有记录并且不想使用开始/救援时获得零
Thanks
谢谢
回答by apneadiving
Yes, just do:
是的,只需这样做:
Challenge.find_by_id(10)
For Rails 4 and 5:
对于 Rails 4 和 5:
Challenge.find_by(id: 10)
回答by hattila91
In Rails 4, dynamic finders - such as find_by_idwhich was used in the accepted answer - were deprecated.
在 Rails 4 中,find_by_id不推荐使用动态查找器(例如在已接受的答案中使用的)。
Moving forward, you should use the new syntax:
继续前进,您应该使用新语法:
Challenge.find_by id: 10
回答by beanie
you can do this a bit hackish, just use the ActiveRecord Query Interface.
你可以这样做有点hackish,只需使用ActiveRecord Query Interface。
this will return nil, instead of raising a Exception
这将返回 nil,而不是引发异常
User.where(:id => 10).first
回答by morgler
Why don't you simply catch the exception? Your case looks exactly like what exceptions were made for:
为什么不简单地捕获异常?您的案例看起来与例外情况完全一样:
begin
user = User.find(10)
rescue ActiveRecord::RecordNotFound
puts "some error msg"
end
If you want to recover from the error in the rescue block (e.g. by setting a placeholder user (null pattern)), you can continue with your code below this block. Otherwise you might just put all your code for the "happy case" in the block between "begin" and "rescue".
如果您想从救援块中的错误中恢复(例如,通过设置占位符用户(空模式)),您可以继续在该块下方执行您的代码。否则,您可能只是将“快乐案例”的所有代码放在“开始”和“救援”之间的块中。
回答by Cristiano Mendon?a
For those struggling with mongoid, it turns out that both findand find_bymethods will raise exception - no matter your rails version!
对于那些挣扎mongoid,事实证明,这两个find和find_by方法将引发异常-无论你的Rails版本!
There is an option(namely raise_not_found_error) which can be set to false, but when falsey makes findmethod doesn't to raise exception as well.
有一个选项(即raise_not_found_error)可以设置为 false,但是当 falsey 使find方法也不会引发异常时。
Thus the solution for mongoid users is the disgusting code:
因此 mongoid 用户的解决方案是恶心的代码:
User.where(id: 'your_id').first # argghhh
回答by tonymarschall
You can try this Challenge.exists?(10)
你可以试试这个 Challenge.exists?(10)
回答by mohamed-ibrahim
just as simple as:
就这么简单:
user = User.find(10) rescue nil
回答by Alanoud Just
You can use find_by with the required attribute (in your case the id) this will return nil instead of giving an error if the given id is not found.
您可以将 find_by 与所需的属性(在您的情况下为 id)一起使用,如果未找到给定的 id,这将返回 nil 而不是给出错误。
user = Challenge.find_by_id(id_value)
or you could use the new format:
或者您可以使用新格式:
user = Challenge.find_by id: id_value
You could also use where but you have to know that where return an active record relation with zero or more records you need to use first to return only one record or nil in case zero records return.
您也可以使用 where 但您必须知道 where 返回一个具有零个或多个记录的活动记录关系,您需要首先使用它来仅返回一个记录或在零记录返回的情况下返回 nil。
user = Challenge.where(id: id_value).first

