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

Feature request; prevent mistakes from creating crazy error messages

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

      Greetings,

      I think it would be a great feature if mongoid would create an method= for embeds_many.

      I assigned a class directly into a embeds_many field and encountered this error:

      /var/lib/gems/1.9.1/gems/mongoid-3.1.4/lib/mongoid/attributes.rb:320:in `method_missing': undefined method `first' for #<Blue _id: 51ba6eed23e5b2905b000001, > (NoMethodError)
              from /var/lib/gems/1.9.1/gems/mongoid-3.1.4/lib/mongoid/relations/embedded/batchable.rb:199:in `normalize_docs'
              from /var/lib/gems/1.9.1/gems/mongoid-3.1.4/lib/mongoid/relations/embedded/batchable.rb:86:in `batch_replace'
              from /var/lib/gems/1.9.1/gems/mongoid-3.1.4/lib/mongoid/relations/embedded/many.rb:313:in `substitute'
              from /var/lib/gems/1.9.1/gems/mongoid-3.1.4/lib/mongoid/relations/accessors.rb:265:in `block (2 levels) in setter'
              from /var/lib/gems/1.9.1/gems/mongoid-3.1.4/lib/mongoid/relations/accessors.rb:175:in `without_autobuild'
              from /var/lib/gems/1.9.1/gems/mongoid-3.1.4/lib/mongoid/relations/accessors.rb:263:in `block in setter'
              from dl.test.rb:23:in `<main>'
      

      If mongoid would create an accessor such as

        embeds_many :blue
      
        # auto created accessor to prevent mistakes
        def blue=(value)
          raise "You cannot assign values to embeds_many fields; you must use blue << newValue"
        end
      

      This would prevent the odd error above and perhaps save others a lot of time trying to figure out how that error message attaches to their code.

      Full list of boo-boo code:

      require 'mongoid'
      
      Mongoid.load!("config/mongoid.yml", :test)
      
      class Inquiry
        include Mongoid::Document
      
        embeds_many :blue
      end
      
      class Blue
        include Mongoid::Document
        embedded_in :inquiry
      end
      
      Inquiry.all.delete
      i = Inquiry.new
      i.blue = Blue.new   # <-=== Bad line of code
      i.save
      

      Thank you!!!!

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

              Created:
              Updated:
              Resolved: