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

Don't update destroyed objects before validation

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

      Hi Guys, first of all, thank you for amazing work that you do.

      I'm using mongoid 3.0.11 and Rails 3.2.3 and I have the following models:

      class Parent
        include Mongoid::Document
      
        field :name, :type => String
      
        embeds_many :children
      
        accepts_nested_attributes_for :children, :allow_destroy => true
      end
      
      class Child
        include Mongoid::Document
      
        field :position, :type => Integer
        field :name, :type => String
      
        validates_uniqueness_of :position
      
        embedded_in :parent
      end
      

      The main detail is that I have a

      Unable to find source-code formatter for language: validates_uniqueness_of. 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
       :position

      . This cause some problems in the follow scenario:

      parent = Parent.create name: "Some Parent"
      
      child_a = Child.new position: 0, name: "A"
      child_b = Child.new position: 2, name: "B"
      child_c = Child.new position: 3, name: "C"
      
      parent.children = [child_a, child_b, child_c]
      parent.save!
      
      parent.update_attributes(
        "children_attributes" => {
          "0" => {
            "id" => child_a._id,
            "name" => "AA",
            "position" => 0
          },
          "1" => {
            "id" => child_b._id,
            "name" => "BB",
            "position" => 1,
            "_destroy" => "1"
          },
          "2" => {
            "id" => child_c._id,
            "name" => "CC",
            "position" => 2
          }
        }
      )
      
      puts parent.valid? # => false
      

      I created three child objects with position 0, 2 and 3, respectively. When I tried to update child_b position and destroy it on the same time, Mongoid does not update the original position of destroyed objects before apply model validations.

      The child_b position and child_c position stay with the same value: 2. So, the

      Unable to find source-code formatter for language: validates_uniqueness_of. 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
       :position

      fails, and my parent object is not saved.

      The behavior does not happen on Mongoid 2.x.x. This is a feature? Make sense this current behavior? Or is it a bug?

      Thank you again.

      Regards.

            Assignee:
            durran Durran Jordan
            Reporter:
            lucasas lucasas
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: