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

Default scope causes NoMethodError: undefined method `*' for nil:NilClass

    • Type: Icon: Task Task
    • Resolution: Done
    • 3.1.5
    • Affects Version/s: None
    • Component/s: None
    • None

      I am getting a weird error and I'm not sure whether it is caused by what I am doing or by Mongoid.

      I have a Booking model with embedded LineItem models. My class definitions look like this (reduced down for clarity).

      class Booking
        include Mongoid::Document
        include Mongoid::Timestamps
        include Mongoid::History::Trackable
      
        track_history track_destroy: true, track_create: true
      
        embeds_many :line_items
      end
      
      class LineItem
        include Mongoid::Document
        include Mongoid::Timestamps
        include Mongoid::History::Trackable
      
        track_history track_destroy: true, track_create: true
      
        field :amount, type: BigDecimal, default: 0
        field :quantity, type: Integer, default: 1
      
        embedded_in :booking
      
        default_scope order_by amount: :desc
      
        belongs_to :item, polymorphic: true
      
        def total_price
          return 0 if amount.blank? || quantity.blank?
          amount * quantity
        end
      end
      

      This is the error and stacktrace that I get:

      Unable to find source-code formatter for language: shell. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      >irb(main):010:0> b = Booking.find(...)
      >irb(main):011:0> b.line_items
      NoMethodError: undefined method `*' for nil:NilClass
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/mongoid-3.0.3/lib/mongoid/contextual/memory.rb:382:in `block (2 levels) in in_place_sort'
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/mongoid-3.0.3/lib/mongoid/contextual/memory.rb:379:in `sort!'
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/mongoid-3.0.3/lib/mongoid/contextual/memory.rb:379:in `block in in_place_sort'
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/mongoid-3.0.3/lib/mongoid/contextual/memory.rb:378:in `each_pair'
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/mongoid-3.0.3/lib/mongoid/contextual/memory.rb:378:in `in_place_sort'
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/mongoid-3.0.3/lib/mongoid/contextual/memory.rb:344:in `apply_sorting'
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/mongoid-3.0.3/lib/mongoid/contextual/memory.rb:168:in `initialize'
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/mongoid-3.0.3/lib/mongoid/contextual.rb:47:in `new'
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/mongoid-3.0.3/lib/mongoid/contextual.rb:47:in `create_context'
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/mongoid-3.0.3/lib/mongoid/contextual.rb:30:in `context'
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/mongoid-3.0.3/lib/mongoid/contextual.rb:20:in `rescue in each'
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/mongoid-3.0.3/lib/mongoid/contextual.rb:18:in `each'
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/mongoid-3.0.3/lib/mongoid/relations/embedded/many.rb:432:in `entries'
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/mongoid-3.0.3/lib/mongoid/relations/embedded/many.rb:432:in `scope'
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/mongoid-3.0.3/lib/mongoid/relations/embedded/many.rb:224:in `block in initialize'
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/mongoid-3.0.3/lib/mongoid/relations/proxy.rb:38:in `init'
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/mongoid-3.0.3/lib/mongoid/relations/embedded/many.rb:218:in `initialize'
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/mongoid-3.0.3/lib/mongoid/relations/accessors.rb:44:in `new'
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/mongoid-3.0.3/lib/mongoid/relations/accessors.rb:44:in `create_relation'
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/mongoid-3.0.3/lib/mongoid/relations/accessors.rb:26:in `__build__'
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/mongoid-3.0.3/lib/mongoid/relations/accessors.rb:156:in `block (3 levels) in getter'
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/mongoid-3.0.3/lib/mongoid/threaded/lifecycle.rb:125:in `_loading'
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/mongoid-3.0.3/lib/mongoid/relations/accessors.rb:156:in `block (2 levels) in getter'
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/mongoid-3.0.3/lib/mongoid/threaded/lifecycle.rb:84:in `_building'
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/mongoid-3.0.3/lib/mongoid/relations/accessors.rb:155:in `block in getter'
        from (irb):11
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
        from /Users/jord/.rbenv/versions/1.9.3-p385/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
        from script/rails:6:in `require'
      

      If I remove the default_scope line, the error goes away.

      Am I doing something wrong or is this a bug?

            Assignee:
            Unassigned Unassigned
            Reporter:
            jordelver jordelver
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: