Skip to content

Commit 8e231b7

Browse files
[Bug fix] Clear changes after update (#12)
1 parent 1a6248e commit 8e231b7

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
activerecord_batch_update (1.0.0)
4+
activerecord_batch_update (1.0.1)
55
activerecord (>= 7.0, < 8.1)
66
activesupport (>= 7.0, < 8.1)
77

activerecord_batch_update.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Gem::Specification.new do |s|
44
s.name = 'activerecord_batch_update'
5-
s.version = '1.0.0'
5+
s.version = '1.0.1'
66
s.summary = 'Update multiple records with different values in a small number of queries'
77
s.description = ''
88
s.authors = ['Quentin de Metz']

lib/activerecord_batch_update.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +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 { _1.clear_attribute_changes(columns) }
3839

3940
updated_count
4041
end

spec/activerecord_batch_update_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,31 @@
180180
.and(change { cat2.reload.birthday })
181181
end
182182

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)
187+
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
206+
end
207+
183208
context 'when some fields are encrypted' do
184209
let!(:cat1) { Cat.create!(name: 'Felix', birthday: Date.new(2010, 1, 1)) }
185210

0 commit comments

Comments
 (0)