Ruby-on-rails 检查 Rails 中的控制器是否存在记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16682699/
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 exists from controller in Rails
提问by MrYoshiji
In my app a User can create a Business. When they trigger the indexaction in my BusinessesControllerI want to check if a Business is related to the current_user.id:
在我的应用程序中,用户可以创建业务。当他们触发index我的操作时,我BusinessesController想检查一个业务是否与以下内容相关current_user.id:
- If yes: display the business.
- If no: redirect to the
newaction.
- 如果是:显示业务。
- 如果否:重定向到
new操作。
I was trying to use this:
我试图使用这个:
if Business.where(:user_id => current_user.id) == nil
# no business found
end
But it always returns true even when the business doesn't exist...
但即使业务不存在,它也总是返回 true ......
How can I test if a record exists in my database?
如何测试我的数据库中是否存在记录?
回答by MrYoshiji
Why your code does not work?
为什么你的代码不起作用?
The wheremethod returns an ActiveRecord::Relationobject (acts like an array which contains the results of the where), it can be empty but it will never be nil.
该where方法返回一个ActiveRecord::Relation对象(就像一个包含 的结果的数组where),它可以是空的,但永远不会是nil。
Business.where(id: -1)
#=> returns an empty ActiveRecord::Relation ( similar to an array )
Business.where(id: -1).nil? # ( similar to == nil? )
#=> returns false
Business.where(id: -1).empty? # test if the array is empty ( similar to .blank? )
#=> returns true
How to test if at least one record exists?
如何测试是否至少存在一条记录?
Option 1:Using .exists?
选项 1:使用.exists?
if Business.exists?(user_id: current_user.id)
# same as Business.where(user_id: current_user.id).exists?
# ...
else
# ...
end
Option 2:Using .present?(or .blank?, the opposite of .present?)
选项 2:使用.present?(或.blank?,相反.present?)
if Business.where(:user_id => current_user.id).present?
# less efficiant than using .exists? (see generated SQL for .exists? vs .present?)
else
# ...
end
Option 3:Variable assignment in the if statement
选项 3:if 语句中的变量赋值
if business = Business.where(:user_id => current_user.id).first
business.do_some_stuff
else
# do something else
end
This option can be considered a code smell by some linters (Rubocop for example).
某些 linters(例如 Rubocop)可以将此选项视为代码异味。
Option 3b:Variable assignment
选项 3b:变量分配
business = Business.where(user_id: current_user.id).first
if business
# ...
else
# ...
end
You can also use .find_by_user_id(current_user.id)instead of .where(...).first
您也可以使用.find_by_user_id(current_user.id)代替.where(...).first
Best option:
最佳选择:
- If you don't use the
Businessobject(s): Option 1 - If you need to use the
Businessobject(s): Option 3
- 如果您不使用
Business对象:选项 1 - 如果您需要使用
Business对象:选项 3
回答by Hamed
In this case I like to use the exists?method provided by ActiveRecord:
在这种情况下,我喜欢使用exists?ActiveRecord 提供的方法:
Business.exists? user_id: current_user.id
回答by user2427993
with 'exists?':
与“存在?”:
Business.exists? user_id: current_user.id #=> 1 or nil
with 'any?':
与“任何?”:
Business.where(:user_id => current_user.id).any? #=> true or false
If you use something with .where, be sure to avoid trouble with scopes and better use .unscoped
如果你使用.where 的东西,一定要避免作用域的问题,最好使用 .unscoped
Business.unscoped.where(:user_id => current_user.id).any?
回答by Puhlze
ActiveRecord#where will return an ActiveRecord::Relation object (which will never be nil). Try using .empty? on the relation to test if it will return any records.
ActiveRecord#where 将返回一个 ActiveRecord::Relation 对象(永远不会为零)。尝试使用 .empty?在关系上测试它是否会返回任何记录。
回答by marimaf
When you call Business.where(:user_id => current_user.id)you will get an array. This Array may have no objects or one or many objects in it, but it won't be null. Thus the check == nil will never be true.
当你打电话时,Business.where(:user_id => current_user.id)你会得到一个数组。这个数组可能没有对象,也可能有一个或多个对象,但它不会为空。因此 check == nil 永远不会是真的。
You can try the following:
您可以尝试以下操作:
if Business.where(:user_id => current_user.id).count == 0
So you check the number of elements in the array and compare them to zero.
因此,您检查数组中的元素数量并将它们与零进行比较。
or you can try:
或者你可以尝试:
if Business.find_by_user_id(current_user.id).nil?
this will return one or nil.
这将返回一或零。
回答by Nino van Hooff
business = Business.where(:user_id => current_user.id).first
if business.nil?
# no business found
else
# business.ceo = "me"
end
回答by user3633260
I would do it this way if you needed an instance variable of the object to work with:
如果您需要使用对象的实例变量,我会这样做:
if @business = Business.where(:user_id => current_user.id).first
#Do stuff
else
#Do stuff
end

