diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-06-19 17:10:06 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-06-19 17:10:06 -0300 |
commit | 9ce59f66490e753809b6942001a9ffa8bf385bc1 (patch) | |
tree | 59d8d2eff0f4f8cfeee52155a6540e23585e14b2 /activerecord/lib/active_record/associations | |
parent | 8418151409e01aa44d8fc9df53e476ce851bad8e (diff) | |
parent | e6690d37204dbd7154d275df75d9d0f98f5d2526 (diff) | |
download | rails-9ce59f66490e753809b6942001a9ffa8bf385bc1.tar.gz rails-9ce59f66490e753809b6942001a9ffa8bf385bc1.tar.bz2 rails-9ce59f66490e753809b6942001a9ffa8bf385bc1.zip |
Merge pull request #15747 from sgrif/sg-trolololol-this-is-so-broken
Always update counter caches in memory when adding records
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_association.rb | 26 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_through_association.rb | 1 |
2 files changed, 26 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 2727e23870..477888228d 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -83,6 +83,13 @@ module ActiveRecord if has_cached_counter?(reflection) counter = cached_counter_attribute_name(reflection) owner.class.update_counters(owner.id, counter => difference) + update_counter_in_memory(difference, reflection) + end + end + + def update_counter_in_memory(difference, reflection = reflection()) + if has_cached_counter?(reflection) + counter = cached_counter_attribute_name(reflection) owner[counter] += difference owner.changed_attributes.delete(counter) # eww end @@ -137,6 +144,25 @@ module ActiveRecord false end end + + def concat_records(records, *) + update_counter_if_success(super, records.length) + end + + def _create_record(attributes, *) + if attributes.is_a?(Array) + super + else + update_counter_if_success(super, 1) + end + end + + def update_counter_if_success(saved_successfully, difference) + if saved_successfully + update_counter_in_memory(difference) + end + saved_successfully + end end end end diff --git a/activerecord/lib/active_record/associations/has_many_through_association.rb b/activerecord/lib/active_record/associations/has_many_through_association.rb index 35e176622d..af38f2f6dd 100644 --- a/activerecord/lib/active_record/associations/has_many_through_association.rb +++ b/activerecord/lib/active_record/associations/has_many_through_association.rb @@ -63,7 +63,6 @@ module ActiveRecord end save_through_record(record) - update_counter(1) record end |