Skip to content

Commit 2b7f703

Browse files
Add kwarg to prevent updated attribute clearing (#13)
1 parent 8e231b7 commit 2b7f703

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
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.1)
4+
activerecord_batch_update (1.0.2)
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.1'
5+
s.version = '1.0.2'
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module ActiveRecordBatchUpdate
1414
# which will re-insert the objects if they were deleted in another thread
1515

1616
module ClassMethods
17-
def batch_update(entries, columns:, batch_size: 100, validate: true)
17+
def batch_update(entries, columns:, batch_size: 100, validate: true, clear_attribute_changes: true)
1818
columns = column_names if columns == :all
1919
columns = (Array.wrap(columns).map(&:to_s) + %w[updated_at]).uniq
2020

@@ -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 { _1.clear_attribute_changes(columns) }
38+
entries.each { _1.clear_attribute_changes(columns) } if clear_attribute_changes
3939

4040
updated_count
4141
end

spec/activerecord_batch_update_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,15 @@
187187

188188
expect(cat1.changes_to_save).to be_empty
189189
end
190+
191+
context 'with the clear_attribute_changes option as false' do
192+
it 'does not clear any changes after the update' do
193+
cat1.name = 'Nala'
194+
Cat.batch_update([cat1], columns: :all, clear_attribute_changes: false)
195+
196+
expect(cat1.changes_to_save.keys).to contain_exactly('name', 'updated_at')
197+
end
198+
end
190199
end
191200

192201
context 'when saving only some of the dirty attributes' do

0 commit comments

Comments
 (0)