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

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

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

      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@mongodb.com Neil Shweky (Inactive)
            Reporter:
            kylemacey kylemacey
            Votes:
            1 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: