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
|