SQL 检查 Rails 中是否不存在记录(来自 id 数组)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4326538/
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
Check if record does NOT exist in Rails (from array of ids)?
提问by Lance Pollard
I can do this to check if a record(s) exists (say id "1" exists, but "2" and "3" don't):
我可以这样做来检查记录是否存在(比如 id “1”存在,但“2”和“3”不存在):
Model.exists?(:id => [1, 2, 3]) #=> true
How do I do the opposite, so:
我如何做相反的事情,所以:
Model.not_exists?(:id => [1, 2, 3]) #=> true
采纳答案by afgomez
If you only need search records through ID you can try this
如果你只需要通过 ID 搜索记录你可以试试这个
class Model
def self.not_exists?(ids)
self.find(ids)
false
rescue
true
end
end
If any of the IDs does not exist the find
method will raise a ActiveRecord::RecordNotFound exception that we simply catch and return true.
如果任何 ID 不存在,该find
方法将引发 ActiveRecord::RecordNotFound 异常,我们只需捕获并返回 true。
Excuse my English :)
原谅我的英语 :)
回答by GSto
just add a ! operator
只需添加一个!操作员
!Model.exists?(:id => [1, 2, 3]) #=> true
回答by Michiel de Mare
class Model
def self.does_not_exist?(ids)
Model.where(id: ids).count < ids.size
end
end
Explanation: If (and only if) all the instances you're looking for exist, Model.where(id: ids).count
is equal to ids.size
.
说明:如果(且仅当)您要查找的所有实例都存在,Model.where(id: ids).count
则等于ids.size
。
However if there are one or more instances missing, the count will be lower, meaning that there's a record that does not exist.
但是,如果缺少一个或多个实例,则计数会更低,这意味着存在不存在的记录。
回答by Sticky
A more Ruby-esque way of doing it would be to use unless
with exists?
. This way, you don't have to use !
. I imagine your use case is something like this:
一种更像 Ruby 风格的方法是使用unless
with exists?
. 这样,您就不必使用!
. 我想你的用例是这样的:
def my_method
return unless Model.exists?(:id => [1, 2, 3])
# do something
end
You can replace 1, 2, 3
with a variable (call it id
or something) and even remove the array altogether if you'd like: .exists?(id: id)
你可以1, 2, 3
用一个变量替换(调用它id
或其他东西),如果你愿意,甚至可以完全删除数组:.exists?(id: id)
回答by Derrick Mar
Another simple way is to use the where method with an array of id's.
另一种简单的方法是使用带有 id 数组的 where 方法。
# If the count of the query is equal to the count of all of the id's then the statement will return false.
# Else it will return true if not all ids exists in the database.
Model.where(id: [1, 2, 3]).count < [1,2,3].count
回答by mwoods79
Use empty?
, this is what you want. It uses count(*)
vs select 1 as one
.
使用empty?
,这就是你想要的。它使用count(*)
vs select 1 as one
。
> Rocketeer.where(:id => [1, 2, 3]).empty?
(0.6ms) SELECT COUNT(*) FROM "rocketeers" WHERE "rocketeers"."id" IN (1, 2, 3)
=> false
> Rocketeer.where(:id => [1, 2, 3]).any?
(0.5ms) SELECT COUNT(*) FROM "rocketeers" WHERE "rocketeers"."id" IN (1, 2, 3)
=> true
> Rocketeer.where(:id => [1, 2, 3]).exists?
Rocketeer Exists (0.5ms) SELECT 1 AS one FROM "rocketeers" WHERE "rocketeers"."id" IN (1, 2, 3) LIMIT 1
=> true