Rails 3.2 Uniqueness validation raises the undefined 'zero?' Method for nil: nilclass

I am using Rails 3.2.0.

I have a simple model as shown below

class Favorite < ActiveRecord::Base validates :lst, :presence => true validates :uuid, :presence => true, :uniqueness => {:scope => :lst} end 

If I try this one

 f = Favorite.new f.valid? 

The following error message appears:

 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!' .... .... 
+6
source share
1 answer

ActiveRecord ACR Tables Allocation Aborted

The error is probably due to ActiveRecord AREL losing how to sum an empty array.

The corresponding line of code is in the alias_tracker.rb file:

  count.sum 

If count is an empty array, then the string is evaluated as:

  [].sum 

In Ruby, which does not work:

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

Rails ActiveSupport Variable # Amount

In Rails, which succeeds as ActiveSupport creates Enumerable # sum

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

Your problem is probably due to the fact that some area of ​​your application that is not related to your areas also creates the sum Enumerable # sum or Array #. Unbound code overwrites the Rails method.

This can happen in your code or in unrelated jewelry. The Rails gem is loaded earlier, usually first in the Gemfile, and any subsequent gem can interfere with Rails.

How to fix it?

Have you written a method called sum, possibly in a module named Enumerable or Array? If so, this is a good place to start. You can rename your method or try to change the method according to the Rails method, replacing the #sum code with this code:

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

If you have not written a method called sum in your code, then the conflict will most likely be used in the gem that you are using. Try using the commenting stones you are using, then restart the application.

You can find a gem that defines a method called sum as follows:

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

Do you use any gems called sixarm? If so, contact me and I will fix them for you. It's me, and some of them define #sum for use with statistical tools and utilities.

Hope this helps. Can you post a message here if it solves your problem?

+8
source

Source: https://habr.com/ru/post/908491/


All Articles