Search code examples
ruby-on-railsrubyruby-debug

NoMethodError undefined method `[]=' for nil:NilClass


I created multiple classes with one test method to test wither the ruby objects get serialized correctly.

The error returned:

undefined method `[]=' for nil:NilClass
    from /Users/horse/workspace/queryapi/app/models/query_model.rb:193:in `serialize'

I run the below test_query method through the rails console by initializing QueryModelTester and then invoking test_query() method on that object.

My code:

class QueryModelTester
  def test_query
    must = Must.new
    range_criteria = RangeCriteria.new 
    range_criteria.gte = 20140712
    range_criteria.lte = 1405134711

    range = RangeBuilder.new
    range.search_field = "created_time"
    range.range_criteria = range_criteria

    must.range = range
    bool = Bool.new
    bool.must = must

    main_query = bool.serialize
    puts main_query
  end
end

Here are the model classes the above class is testing:

class RangeCriteria    
  @query_hash = Hash.new
  attr_accessor :gte, :lte

  def serialize
    if(@gte.present?)
      @query_hash[:gte] = @gte
    end

    if(@lte.present?)
      @query_hash[:lte] = @lte
    end

    if(@gte.present? || @lte.present?) 
      return @query_hash
    end
  end
end


class RangeBuilder
  @query_hash = Hash.new
  attr_accessor :search_field, :range_criteria

  def serialize
    if(@search_field.present?)
      @query_hash[@search_field] = @range_criteria.serialize
      return @query_hash[:range] = @query_hash
    end
  end    
end

class Bool
  @query_hash = {}
  attr_accessor :must

  def serialize
    if( @must.present? )
      @query_hash[:must] = @must.serialize
      return @query_hash[:bool] = @query_hash
    end
  end
end

Solution

  • The problem is when you initialize your @query_hash. In all your classes they are initialized in wrong scope. To fix it, you should move @query_hash = Hash.new to initialize method, like:

    class RangeCriteria
      def initialize
        @query_hash = Hash.new
      end
        # ...
    end
    
    class RangeBuilder
      def initialize
        @query_hash = Hash.new
      end
        # ...
    end
    
    class Bool
      def initialize
        @query_hash = Hash.new
      end
        # ...
    end
    

    Hope that helps.

    Good luck!