6

我在以下代码中遇到异常。有人可以告诉我我做错了什么,以及如何防止它?

def self.find_by_data(data = {})
            where(name_canonical: data['name'].downcase.gsub(/\s+/, ''),
                  fuel:           data['fuel'],
                  trim_canonical: data['trim'].downcase.gsub(/\s+/, ''),
                  year:           data['year']).first
        end

例外:

/Users/charlie/Documents/WIP/projectx/ar_models.rb:35:in `find_by_data': undefined method `downcase' for nil:NilClass (NoMethodError)ooooooooooooooooooooooooo| ETA:   0:00:00
    from /Users/charlie/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:36:in `block in find_by_data'
    from /Users/charlie/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/associations/collection_proxy.rb:845:in `block in scoping'
    from /Users/charlie/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/relation.rb:270:in `scoping'
    from /Users/charlie/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/associations/collection_proxy.rb:845:in `scoping'
    from /Users/charlie/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:36:in `find_by_data'
    from /Users/charlie/Documents/WIP/projectx/ar_models.rb:132:in `create_or_assign_existing'
    from /Users/charlie/Documents/WIP/projectx/app.rb:230:in `block (2 levels) in work'
    from /Users/charlie/.rvm/gems/ruby-2.0.0-p0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
    from /Users/charlie/Documents/WIP/projectx/app.rb:80:in `block in work'
    from /Users/charlie/.rvm/gems/ruby-2.0.0-p0/gems/parallel-0.6.3/lib/parallel.rb:319:in `call'
    from /Users/charlie/.rvm/gems/ruby-2.0.0-p0/gems/parallel-0.6.3/lib/parallel.rb:319:in `call_with_index'
    from /Users/charlie/.rvm/gems/ruby-2.0.0-p0/gems/parallel-0.6.3/lib/parallel.rb:179:in `block (3 levels) in work_in_threads'
    from /Users/charlie/.rvm/gems/ruby-2.0.0-p0/gems/parallel-0.6.3/lib/parallel.rb:326:in `with_instrumentation'
    from /Users/charlie/.rvm/gems/ruby-2.0.0-p0/gems/parallel-0.6.3/lib/parallel.rb:177:in `block (2 levels) in work_in_threads'
    from /Users/charlie/.rvm/gems/ruby-2.0.0-p0/gems/parallel-0.6.3/lib/parallel.rb:171:in `loop'
    from /Users/charlie/.rvm/gems/ruby-2.0.0-p0/gems/parallel-0.6.3/lib/parallel.rb:171:in `block in work_in_threads'
    from /Users/charlie/.rvm/gems/ruby-2.0.0-p0/gems/parallel-0.6.3/lib/parallel.rb:62:in `block (2 levels) in in_threads'
4

3 回答 3

18

当您看到“ ...nil:NilClass 的未定义方法”时,这意味着您有一个nil试图调用该方法的值。

data['name']在这种情况下,没有定义类似的东西。

为了使这更加防弹:

data['name'].to_s.downcase.gsub(/\s+/, '')

这会将所有内容转换为字符串以开始。nil.to_s默认情况下是一个空字符串,所以它是安全的。

于 2013-09-19T18:14:15.820 回答
0

也许使用三元运算符:

def self.find_by_data(data = {})
            where(name_canonical: data['name'] == nil ? '' : data['name'].downcase.gsub(/\s+/, ''),
                  fuel:           data['fuel'],
                  trim_canonical: data['trim'] == nil ? '' : data['name'].downcase.gsub(/\s+/, ''),
                  year:           data['year']).first
        end
于 2013-09-19T18:13:37.393 回答
0

您的

data['name'] 

或者

data['trim']

是零。检查您的输入数据。

于 2013-09-19T18:14:37.727 回答