Rails 3.2唯一性验证引发未定义的方法'零?'为零:Nilclass

时间:2012-02-15 07:38:02

标签: ruby-on-rails validation unique

我使用的是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!'
....
....

1 个答案:

答案 0 :(得分:8)

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用于统计工具和实用工具。

希望这会有所帮助。如果能解决你的问题,你能在这里发帖吗?