Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
activerecord_batch_update (1.0.1)
activerecord_batch_update (1.0.2)
activerecord (>= 7.0, < 8.1)
activesupport (>= 7.0, < 8.1)

Expand Down
2 changes: 1 addition & 1 deletion activerecord_batch_update.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Gem::Specification.new do |s|
s.name = 'activerecord_batch_update'
s.version = '1.0.1'
s.version = '1.0.2'
s.summary = 'Update multiple records with different values in a small number of queries'
s.description = ''
s.authors = ['Quentin de Metz']
Expand Down
4 changes: 2 additions & 2 deletions lib/activerecord_batch_update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module ActiveRecordBatchUpdate
# which will re-insert the objects if they were deleted in another thread

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

Expand All @@ -35,7 +35,7 @@ def batch_update(entries, columns:, batch_size: 100, validate: true)
end

connection.clear_query_cache if connection.query_cache_enabled
entries.each { _1.clear_attribute_changes(columns) }
entries.each { _1.clear_attribute_changes(columns) } if clear_attribute_changes

updated_count
end
Expand Down
9 changes: 9 additions & 0 deletions spec/activerecord_batch_update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@

expect(cat1.changes_to_save).to be_empty
end

context 'with the clear_attribute_changes option as false' do
it 'does not clear any changes after the update' do
cat1.name = 'Nala'
Cat.batch_update([cat1], columns: :all, clear_attribute_changes: false)

expect(cat1.changes_to_save.keys).to contain_exactly('name', 'updated_at')
end
end
end

context 'when saving only some of the dirty attributes' do
Expand Down
Loading