Skip to content

Commit 8b9a081

Browse files
authored
MONGOID-5190 Respect persistence options on auto-saved associations (#5283)
* MONGOID-5190 Respect persistence options on auto-saved associations * MONGOID-5190 unpend test and fix test * MONGOID-5190 format * Update lib/mongoid/clients/options.rb
1 parent 7afc626 commit 8b9a081

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

lib/mongoid/association/referenced/auto_save.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ def self.define_autosave!(association)
5656
__autosaving__ do
5757
if assoc_value = ivar(association.name)
5858
Array(assoc_value).each do |doc|
59-
doc.with(persistence_context) do |d|
59+
pc = doc.persistence_context? ? doc.persistence_context : persistence_context
60+
doc.with(pc) do |d|
6061
d.save
6162
end
6263
end

lib/mongoid/clients/options.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ def persistence_context
4545
PersistenceContext.new(self.class)
4646
end
4747

48+
def persistence_context?
49+
!!(PersistenceContext.get(self) || PersistenceContext.get(self.class))
50+
end
51+
4852
private
4953

5054
def set_persistence_context(options_or_context)

spec/mongoid/association/referenced/belongs_to/proxy_spec.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,6 @@
322322
end
323323

324324
it 'applies the persistence options when saving the child' do
325-
pending 'https://jira.mongodb.org/browse/MONGOID-5190'
326-
327325
expect(other_people_count).to eq(1)
328326
expect(other_posts_count).to eq(1)
329327
end

0 commit comments

Comments
 (0)