diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2017-12-12 16:47:51 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-12 16:47:51 -0500 |
commit | 185a30f75289ab158abd3c21536930c37af61338 (patch) | |
tree | 265389a1c174006be2b5200ff4609ae6b7a31093 /activerecord/lib | |
parent | 2eee8c3bad674bc3170cfac3e8586127560a4ea5 (diff) | |
parent | 6ddba9b46c1d307ea7e03d753d0988183b9cb80c (diff) | |
download | rails-185a30f75289ab158abd3c21536930c37af61338.tar.gz rails-185a30f75289ab158abd3c21536930c37af61338.tar.bz2 rails-185a30f75289ab158abd3c21536930c37af61338.zip |
Merge pull request #31405 from bogdanvlviv/fix-conflicts-counter_cache-with-touch-by-optimistic_locking
Fix conflicts `counter_cache` with `touch: true` by optimistic locking.
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/belongs_to_association.rb | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/builder/belongs_to.rb | 10 |
2 files changed, 9 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb index ba54cd8f49..68c608df13 100644 --- a/activerecord/lib/active_record/associations/belongs_to_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_association.rb @@ -49,9 +49,9 @@ module ActiveRecord def update_counters(by) if require_counter_update? && foreign_key_present? if target && !stale_target? - target.increment!(reflection.counter_cache_column, by) + target.increment!(reflection.counter_cache_column, by, touch: reflection.options[:touch]) else - klass.update_counters(target_id, reflection.counter_cache_column => by) + klass.update_counters(target_id, reflection.counter_cache_column => by, touch: reflection.options[:touch]) end end end diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb index 9904ee4bed..c161454c1a 100644 --- a/activerecord/lib/active_record/associations/builder/belongs_to.rb +++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb @@ -114,9 +114,13 @@ module ActiveRecord::Associations::Builder # :nodoc: BelongsTo.touch_record(record, record.send(changes_method), foreign_key, n, touch, belongs_to_touch_method) }} - model.after_save callback.(:saved_changes), if: :saved_changes? - model.after_touch callback.(:changes_to_save) - model.after_destroy callback.(:changes_to_save) + unless reflection.counter_cache_column + model.after_create callback.(:saved_changes), if: :saved_changes? + model.after_destroy callback.(:changes_to_save) + end + + model.after_update callback.(:saved_changes), if: :saved_changes? + model.after_touch callback.(:changes_to_save) end def self.add_default_callbacks(model, reflection) |