Ruby-on-rails 如何在Array Rails ActiveRecord中无一例外地选择where ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1441791/
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
How to select where ID in Array Rails ActiveRecord without exception
提问by Jakub Arnold
When I have array of ids, like
当我有一组 id 时,比如
ids = [2,3,5]
and I perform
我表演
Comment.find(ids)
everything works fine. But when there is id that doesn't exist, I get an exception. This occurs generaly when I get list of IDs that match some filter and than I do something like
一切正常。但是当有不存在的 id 时,我会得到一个例外。当我获得与某个过滤器匹配的 ID 列表而不是执行类似操作时,通常会发生这种情况
current_user.comments.find(ids)
This time I may have a valid comment ID, which however does not belong to given User, so it is not found and I get an exception.
这次我可能有一个有效的评论 ID,但是它不属于给定的用户,所以没有找到它,我得到了一个例外。
I've tried find(:all, ids), but it returns all of the records.
我试过find(:all, ids),但它返回所有记录。
The only way I can do it now is
我现在唯一能做的就是
current_user.comments.select { |c| ids.include?(c.id) }
But that seems to me like super inefficient solution.
但这在我看来就像是超级低效的解决方案。
Is there better way to select ID in Arraywithout getting exception on non-existing record?
有没有更好的方法来选择Array 中的 ID而不会在不存在的记录上出现异常?
回答by prismofeverything
If it is just avoiding the exception you are worried about, the "find_all_by.." family of functions works without throwing exceptions.
如果只是避免您担心的异常,那么“find_all_by..”系列函数可以在不抛出异常的情况下工作。
Comment.find_all_by_id([2, 3, 5])
will work even if some of the ids don't exist. This works in the
即使某些 id 不存在也能工作。这适用于
user.comments.find_all_by_id(potentially_nonexistent_ids)
case as well.
情况也是如此。
Update: Rails 4
更新:Rails 4
Comment.where(id: [2, 3, 5])
回答by mjnissim
Update: This answer is more relevant for Rails 4.x
更新:此答案与 Rails 4.x 更相关
Do this:
做这个:
current_user.comments.where(:id=>[123,"456","Michael Hymanson"])
The stronger side of this approach is that it returns a Relationobject, to which you can join more .whereclauses, .limitclauses, etc., which is very helpful. It also allows non-existent IDs without throwing exceptions.
这种方式的强点在于它返回一个Relation对象,你可以在其中加入更多的.where子句、.limit子句等,非常有帮助。它还允许不存在的 ID 不抛出异常。
The newer Ruby syntax would be:
较新的 Ruby 语法是:
current_user.comments.where(id: [123, "456", "Michael Hymanson"])
回答by Jonathan Lin
If you need more control (perhaps you need to state the table name) you can also do the following:
如果您需要更多控制(也许您需要说明表名),您还可以执行以下操作:
Model.joins(:another_model_table_name)
.where('another_model_table_name.id IN (?)', your_id_array)
回答by Sumit Munot
Now .find and .find_by_id methods are deprecated in rails 4. So instead we can use below:
现在 .find 和 .find_by_id 方法在 rails 4 中被弃用了。所以我们可以使用下面的方法:
Comment.where(id: [2, 3, 5])
It will work even if some of the ids don't exist. This works in the
即使某些 id 不存在,它也会起作用。这适用于
user.comments.where(id: avoided_ids_array)
Also for excluding ID's
也用于排除 ID
Comment.where.not(id: [2, 3, 5])
回答by rogeriopvl
To avoid exceptions killing your app you should catch those exceptions and treat them the way you wish, defining the behavior for you app on those situations where the id is not found.
为了避免异常杀死您的应用程序,您应该捕获这些异常并按照您希望的方式处理它们,在找不到 id 的情况下为您的应用程序定义行为。
begin
current_user.comments.find(ids)
rescue
#do something in case of exception found
end
Here's more infoon exceptions in ruby.
回答by mtfk
You can also use it in named_scope if You want to put there others conditions
如果你想把其他条件放在那里,你也可以在 named_scope 中使用它
for example include some other model:
例如包括一些其他模型:
named_scope 'get_by_ids', lambda { |ids| { :include => [:comments], :conditions => ["comments.id IN (?)", ids] } }
named_scope 'get_by_ids', lambda { |ids| { :include => [:comments], :conditions => ["comments.id IN (?)", ids] } }

