ruby-on-rails – Rails 3.2唯一性验证引发未定义的方法’零?’为零:Nilclass

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Rails 3.2唯一性验证引发未定义的方法’零?’为零:Nilclass前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Rails 3.2.0.

我有一个简单的模型,如下所示

class Favorite < ActiveRecord::Base

  validates :lst,:presence => true
  validates :uuid,:presence => true,:uniqueness => {:scope => :lst}                                                                                                                                                                    
end

如果我试试这个

f = Favorite.new
f.valid?

我收到以下错误消息:

NoMethodError: undefined method `zero?' for nil:NilClass
    from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.0/lib/active_record/associations/alias_tracker.rb:28:in `aliased_name_for'
    from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.0/lib/active_record/associations/join_dependency.rb:17:in `initialize'
    from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.0/lib/active_record/relation/finder_methods.rb:219:in `new'
    from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.0/lib/active_record/relation/finder_methods.rb:219:in `construct_join_dependency_for_association_find'
    from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.0/lib/active_record/relation/finder_methods.rb:192:in `exists?'
    from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.0/lib/active_record/validations/uniqueness.rb:32:in `validate_each'
    from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activemodel-3.2.0/lib/active_model/validator.rb:153:in `block in validate'
    from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activemodel-3.2.0/lib/active_model/validator.rb:150:in `each'
    from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activemodel-3.2.0/lib/active_model/validator.rb:150:in `validate'
    from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.0/lib/active_support/callbacks.rb:310:in `_callback_before_15'
    from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.0/lib/active_support/callbacks.rb:429:in `_run__1275595979440079611__validate__42615372200132002__callbacks'
    from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.0/lib/active_support/callbacks.rb:405:in `__run_callback'
    from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.0/lib/active_support/callbacks.rb:385:in `_run_validate_callbacks'
    from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.0/lib/active_support/callbacks.rb:81:in `run_callbacks'
    from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activemodel-3.2.0/lib/active_model/validations.rb:212:in `run_validations!'
    from /Users/ragrawal/.rvm/gems/ruby-1.9.2-p290/gems/activemodel-3.2.0/lib/active_model/validations/callbacks.rb:53:in `block in run_validations!'
....
....

解决方法

ActiveRecord AREL表别名正在破坏

错误可能是由于ActiveRecord AREL失去了如何对空数组求和的记录.

相关的代码行位于文件alias_tracker.rb中:

count.sum

如果count是一个空数组,那么该行的计算结果如下:

[].sum

在Ruby中失败:

$irb
 > [].sum
 NoMethodError: undefined method `sum' for []:Array

Rails ActiveSupport Enumerable#sum

在Rails中成功,因为ActiveSupport正在创建Enumerable#sum

$irb
 > require 'active_support/core_ext/enumerable'
 > [].sum  
 => 0

您的问题可能是您的应用程序的某些不相关区域也在创建Enumerable#sum或Array#sum.不相关的代码覆盖了Rails方法.

可能发生在您的代码或不相关的gem中. Rails gem早期加载,通常首先在你的Gemfile中加载,任何后来的gem都会干扰Rails.

怎么解决

你有没有写过一个名为sum的方法,可能在一个名为Enumerable或Array的模块中?如果是这样,那是一个很好的起点.您可以重命名您的方法,或者您可以尝试使用以下代码替换#sum代码来更改您的方法以匹配Rails方法

module Enumerable
  def sum(identity = 0,&block)
    if block_given?
      map(&block).sum(identity)
    else
      inject(:+) || identity
    end
  end
end

如果你还没有在你的代码中编写一个名为sum的方法,那么冲突就可能出现在你正在使用的gem中.尝试评论您正在使用的宝石,然后重新加载您的应用.

您可以搜索定义名为sum的方法的gem,如下所示:

$cd /usr/lib/ruby/gems 
$find | xargs grep "def sum\b"

你在使用名为sixarm的任何宝石吗?如果是这样,请与我联系,我会为您解决.这些是由我来完成的,其中一些确实定义了#sum用于统计工具和实用程序.

希望这可以帮助.如果能解决你的问题,你能在这里发帖吗?

原文链接:https://www.f2er.com/ruby/264954.html

猜你在找的Ruby相关文章