Ruby - NoMethodError(nil:NilClass 的未定义方法):

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21694744/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:20:55  来源:igfitidea点击:

Ruby - NoMethodError (undefined method for nil:NilClass):

ruby-on-railsexceptionnullnomethoderror

提问by Dodinas

I'm having some difficulty trying to figure out what I am doing wrong. My ActiveRecordquery is returning nilwhich I think is causing NoMethodErrorto be raised.

我在试图弄清楚我做错了什么时遇到了一些困难。我的ActiveRecord查询正在返回nil,我认为这会导致NoMethodError提出。

Here's what I have:

这是我所拥有的:

@number_exclude_list = ["1234567890", "2223334545"] 
@available_number = UserNumber.where.not(:user_num => @number_exclude_list)

Which returns an empty set of rows:

它返回一组空的行:

=> #<ActiveRecord::Relation []>

So, then I have:

那么,我有:

if (@available_number.empty?)
   inform_user_empty
else
   do_my_other_function

But, I get:

但是,我得到:

`NoMethodError (undefined method 'inform_user_empty' for nil:NilClass)`

I have tried: @available_number.blank?and `@available_number.nil?

我试过:@available_number.blank?`@available_number.nil?

And I still get the same NoMethodError. Any ideas on what I am doing wrong here?

而且我仍然得到相同的结果NoMethodError。关于我在这里做错了什么的任何想法?

回答by Малъ Скрылевъ

The exception text NoMethodError (undefined method 'inform_user_empty' for nil:NilClass)says that is was a call to instance method #inform_user_emptyof the nilclass'es instance, and since nilhas no instance method rubyinterpreter throwed that exception. I see the two main reason for it:

异常文本NoMethodError (undefined method 'inform_user_empty' for nil:NilClass)说这是对类实例的实例方法#inform_user_empty的调用nil,并且由于nil没有实例方法ruby解释器抛出了该异常。我认为有两个主要原因:

  1. selfkeyword variable has nilvalue, I believe not in the reason, because you do a call from a controller, as you've said ApplicationController. To make sure that selfisn't nil, change the code to the following one:

    if @available_number.empty?
       p self
       self.inform_user_empty
    

    rerun the action, and look at the result.

  2. The exception has been thrown from another place. So you have to specify the full trace log in your post.

  1. self关键字变量具有nil价值,我不相信原因,因为正如您所说,您从控制器进行调用ApplicationController。要确保self不是nil,请将代码更改为以下代码:

    if @available_number.empty?
       p self
       self.inform_user_empty
    

    重新运行该操作,然后查看结果。

  2. 异常已从另一个地方抛出。所以你必须在你的帖子中指定完整的跟踪日志。

回答by Rajesh Omanakuttan

Please run the below line in your console:

请在您的控制台中运行以下行:

@available_number = UserNumber.where.not(:user_num => @number_exclude_list)

it returns an ArgumentError: wrong number of arguments (0 for 1+)since it is not the way to check NOT INcondition in Rails activerecord.

它返回一个,ArgumentError: wrong number of arguments (0 for 1+)因为它不是NOT IN在 Rails activerecord 中检查条件的方法。

Replace it with:

替换为:

User.where('user_num NOT IN (?)',@number_exclude_list)

and then do:

然后做:

if @available_number == nil
   inform_user_empty
else
   do_my_other_function
end

Hoep that will resolve the issue. Please let me know if it really helped you.

希望能解决问题。请让我知道它是否真的对你有帮助。