diff options
author | Matthew Robertson <matthewrobertson03@gmail.com> | 2013-04-21 12:49:50 -0700 |
---|---|---|
committer | Matthew Robertson <matthewrobertson03@gmail.com> | 2013-04-21 18:34:11 -0700 |
commit | 1e27f1c5d5d177089fbda03ba31f2f55fa622a39 (patch) | |
tree | b95d040058ff2308eebca0a542bd651cead55fa6 /activerecord/lib | |
parent | fae07a81badb3067a3ebdd7d81aa22625186329e (diff) | |
download | rails-1e27f1c5d5d177089fbda03ba31f2f55fa622a39.tar.gz rails-1e27f1c5d5d177089fbda03ba31f2f55fa622a39.tar.bz2 rails-1e27f1c5d5d177089fbda03ba31f2f55fa622a39.zip |
Update counter cache when pushing into association
This commit fixes a regression bug in which counter_cache columns
were not being updated correctly when newly created records were
being pushed into an assocation. EG:
# this was fine
@post.comment.create!
# this was fine
@comment = Comment.first
@post.comments << @comment
# this would not update counters
@post.comments << Comment.create!
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/builder/belongs_to.rb | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb index 3ba6a71366..092230b2f7 100644 --- a/activerecord/lib/active_record/associations/builder/belongs_to.rb +++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb @@ -25,9 +25,10 @@ module ActiveRecord::Associations::Builder mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 def belongs_to_counter_cache_after_create_for_#{name} - record = #{name} - record.class.increment_counter(:#{cache_column}, record.id) unless record.nil? - @_after_create_counter_called = true + if record = #{name} + record.class.increment_counter(:#{cache_column}, record.id) + @_after_create_counter_called = true + end end def belongs_to_counter_cache_before_destroy_for_#{name} |