Skip to content

Commit 4cd930b

Browse files
only clear updated dirty attributes
1 parent 42f0199 commit 4cd930b

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

lib/activerecord_batch_update.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def batch_update(entries, columns:, batch_size: 100, validate: true)
3535
end
3636

3737
connection.clear_query_cache if connection.query_cache_enabled
38-
entries.each(&:changes_applied)
38+
entries.each { _1.clear_attribute_changes(columns) }
3939

4040
updated_count
4141
end

spec/activerecord_batch_update_spec.rb

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,29 @@
180180
.and(change { cat2.reload.birthday })
181181
end
182182

183-
it 'clears the changes after the update' do
184-
cat1.name = 'Nala'
185-
Cat.batch_update([cat1], columns: :all)
183+
context 'when saving all dirty attributes' do
184+
it 'clears all changes after the update' do
185+
cat1.name = 'Nala'
186+
Cat.batch_update([cat1], columns: :all)
186187

187-
expect(cat1.changes_to_save).to be_empty
188+
expect(cat1.changes_to_save).to be_empty
189+
end
190+
end
191+
192+
context 'when saving only some of the dirty attributes' do
193+
it 'clears changes made during the update' do
194+
cat1.assign_attributes(
195+
name: 'Nala',
196+
birthday: Date.new(2015, 1, 1)
197+
)
198+
Cat.batch_update([cat1], columns: :name)
199+
200+
expect(cat1.changes_to_save).to eq(
201+
{
202+
'birthday' => [Date.new(2010, 1, 1), Date.new(2015, 1, 1)]
203+
}
204+
)
205+
end
188206
end
189207

190208
context 'when some fields are encrypted' do

0 commit comments

Comments
 (0)