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

Embedded documents are destroyed when validation fails

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

      Hi everybody,

      I have the following models:

      class Jorge
        include Mongoid::Document
      
        field :title, :type => String
      
        embeds_many :tadeus
        accepts_nested_attributes_for :tadeus, :allow_destroy => true
      
        validates_presence_of :title
      end
      
      class Tadeu
        include Mongoid::Document
      
        field :title, :type => String
      
        embedded_in :jorge
      end
      
      

      And besides that, I create the following objects:

      j = Jorge.new
      j.tadeus << Tadeu.new
      j.title = "pedra sobre pedra"
      j.save
      

      And my object, of course, is valid.

      j.reload
      puts j.valid? # true
      

      Now, I'll clean title attribute and after that I'll update

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

      ruby
      j.title = nil
      j.update_attributes :tadeus_attributes => {"0" => {:_id => j.tadeus.first.id, :_destroy => true}}

      Now, my object is invalid, of course:
      
      

      ruby
      puts j.valid? # true

      And the problem is here. Even my Jorge object still invalid, the "tadeus" relation are empty:
      
      

      ruby
      puts j.tadeus.inspect # []
      `

      Is it a bug, isn it?

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

              Created:
              Updated:
              Resolved: