Don't mark embedded objects as persisted when the parent is invalid

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Done
    • Priority: Major - P3
    • 8.0.1
    • Affects Version/s: None
    • Component/s: Associations, Persistence
    • None
    • Minor Change
    • None
    • None
    • None
    • None
    • None
    • None

      When saving a child embedded document, even if the parent is invalid and does not get persisted to the database, Mongoid tells us that the child is still persisted (even though it can't possibly be saved anywhere).

      class School
        include Mongoid::Document
        
        embeds_many :students
        
        field :name, type: :string
        
        validates :name, presence: true
      end
      
      class Student
        include Mongoid::Document
        
        embedded_in :school
      end
      
      [5] pry(main)> school = School.new
      => #<School _id: 534eee6e6b796c75a9000000, name: nil>
      [6] pry(main)> student = school.students.new
      => #<Student _id: 534eee6e6b796c75a9010000, >
      [7] pry(main)> student.save
      => true
      [8] pry(main)> school.persisted?
      => false
      [9] pry(main)> student.persisted?
      => true
      [10] pry(main)> School.count
      => 0
      

            Assignee:
            Neil Shweky (Inactive)
            Reporter:
            kylemacey
            Votes:
            1 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: