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

Using safely.create! raises NoMethodError on invalid objects

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

      Using safe mode with create! (Tag.safely.create! below) seems to cause NoMethodErrors. Example with stack trace below, I tried it on mongoid master, seems to also happen on mongoid rc7.

      <pre>
      class Tag
      include Mongoid::Document
      field :name, :type => String

      validates :name, :format => {:with => /^[a-z -]+$/}
      end
      </pre>

      <pre>
      ruby-1.9.2-p136 :003 > Tag.safely.create!
      NoMethodError: undefined method `merge' for #<Tag _id: 4d6705aa19418f42b4000003, name: nil>
      from /home/mark/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/mongoid/attributes.rb:199:in `method_missing'
      from /home/mark/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/mongoid/safety.rb:173:in `method_missing'
      from /home/mark/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/mongoid/safety.rb:94:in `block in create!'
      from /home/mark/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/mongoid/safety.rb:93:in `tap'
      from /home/mark/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/mongoid/safety.rb:93:in `create!'
      from (irb):3
      from /home/mark/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/commands/console.rb:44:in `start'
      from /home/mark/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/commands/console.rb:8:in `start'
      from /home/mark/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/commands.rb:23:in `<top (required)>'
      from script/rails:6:in `require'
      from script/rails:6:in `<main>'
      </pre>
      expected behavior:
      <pre>
      ruby-1.9.2-p136 :002 > Tag.create!
      Mongoid::Errors::Validations: Validation failed - Name is invalid.
      from /home/mark/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/mongoid/persistence.rb:234:in `fail_validate!'
      from /home/mark/.rvm/gems/ruby-1.9.2-p136/gems/mongoid-2.0.0.rc.7/lib/mongoid/persistence.rb:182:in `create!'
      from (irb):2
      from /home/mark/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/commands/console.rb:44:in `start'
      from /home/mark/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/commands/console.rb:8:in `start'
      from /home/mark/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/commands.rb:23:in `<top (required)>'
      from script/rails:6:in `require'
      from script/rails:6:in `<main>'
      </pre>

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

              Created:
              Updated:
              Resolved: