Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-1679

def initialize causes nil error with Mongoid::Document (simple class)

    • Type: Icon: Task Task
    • Resolution: Done
    • 12_01_17
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      I saw a strange bug today and created a simple class to test it:

      class Alpha
        
        def initialize
           puts 'initialize Alpha'
        end
      end
      

      This class inits fine (Alpha.new). But if I add "include Mongoid::Document" I get this error:

      >> Alpha.new
      initialize Alpha
      NoMethodError: You have a nil object when you didn't expect it!
      You might have expected an instance of Array.
      The error occurred while evaluating nil.[]
      	from /home/user/.rvm/gems/ruby-1.9.2-p290/gems/mongoid-2.4.3/lib/mongoid/inspection.rb:32:in `block in inspect_fields'
      

      Mongoid 2.4.3. The full failing source is:

      class Alpha
        include Mongoid::Document
      
        def initialize
           puts 'initialize Alpha'
        end
      end
      
      

      >> Rails::VERSION::STRING
      => "3.1.1"
      >> RUBY_VERSION
      => "1.9.2"

      I thought adding a simple field (field :name, type: String) might fix it but didn't change anything.

            Assignee:
            Unassigned Unassigned
            Reporter:
            jcollum Justin Collum
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: