Skip to content

Commit f16b297

Browse files
authored
Revert "MONGOID-5822: Embedded association validations break when association depends on parent update (#5922)" (#6012)
This reverts commit ec18c8b. MONGOID-5822 introduced an unacceptable performance regression.
1 parent 7e9d95a commit f16b297

File tree

2 files changed

+1
-61
lines changed

2 files changed

+1
-61
lines changed

lib/mongoid/validatable/associated.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def validate_association(document, attribute)
7474
# use map.all? instead of just all?, because all? will do short-circuit
7575
# evaluation and terminate on the first failed validation.
7676
list.map do |value|
77-
if value && !value.flagged_for_destroy?
77+
if value && !value.flagged_for_destroy? && (!value.persisted? || value.changed?)
7878
value.validated? ? true : value.valid?
7979
else
8080
true

spec/mongoid/association_spec.rb

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -115,66 +115,6 @@ class AssocationSpecModel
115115
expect(name).to_not be_an_embedded_many
116116
end
117117
end
118-
119-
context "when validation depends on association" do
120-
before(:all) do
121-
class Author
122-
include Mongoid::Document
123-
embeds_many :books, cascade_callbacks: true
124-
field :condition, type: Boolean
125-
end
126-
127-
class Book
128-
include Mongoid::Document
129-
embedded_in :author
130-
validate :parent_condition_is_not_true
131-
132-
def parent_condition_is_not_true
133-
return unless author&.condition
134-
errors.add :base, "Author condition is true."
135-
end
136-
end
137-
138-
Author.delete_all
139-
Book.delete_all
140-
end
141-
142-
let(:author) { Author.new }
143-
let(:book) { Book.new }
144-
145-
context "when author is not persisted" do
146-
it "is valid without books" do
147-
expect(author.valid?).to be true
148-
end
149-
150-
it "is valid with a book" do
151-
author.books << book
152-
expect(author.valid?).to be true
153-
end
154-
155-
it "is not valid when condition is true with a book" do
156-
author.condition = true
157-
author.books << book
158-
expect(author.valid?).to be false
159-
end
160-
end
161-
162-
context "when author is persisted" do
163-
before do
164-
author.books << book
165-
author.save
166-
end
167-
168-
it "remains valid initially" do
169-
expect(author.valid?).to be true
170-
end
171-
172-
it "becomes invalid when condition is set to true" do
173-
author.update_attributes(condition: true)
174-
expect(author.valid?).to be false
175-
end
176-
end
177-
end
178118
end
179119

180120
describe "#embedded_one?" do

0 commit comments

Comments
 (0)