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

Embedded objects get marked as persisted even when the parent is invalid

    XMLWordPrintable

Details

    • Bug
    • Status: Backlog
    • Major - P3
    • Resolution: Unresolved
    • None
    • None
    • Associations, Persistence
    • None

    Description

      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
      

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              kylemacey kylemacey
              Votes:
              1 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated: