From 455d710242f24f0cfff89f626164493276e0f3e9 Mon Sep 17 00:00:00 2001 From: John Wang Date: Fri, 15 Mar 2013 11:48:53 -0400 Subject: If a counter_cache is defined, then using update_attributes and changing the primary key on an association will make sure that the corresponding counter on the association is changed properly. Fixes #9722. --- activerecord/CHANGELOG.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'activerecord/CHANGELOG.md') diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index a8151cd23e..79ca34d89d 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,31 @@ ## Rails 4.0.0 (unreleased) ## +* Counter caches on associations will now stay valid when attributes are + updated (not just when records are created or destroyed), for example, + when calling +update_attributes+. The following code now works: + + class Comment < ActiveRecord::Base + belongs_to :post, counter_cache: true + end + + class Post < ActiveRecord::Base + has_many :comments + end + + post = Post.create + comment = Comment.create + + post.comments << comment + post.save.reload.comments_count # => 1 + comment.update_attributes(:post_id => nil) + + post.save.reload.comments_count # => 0 + + Updating the id of a +belongs_to+ object with the id of a new object will + also keep the count accurate. + + *John Wang* + * Referencing join tables implicitly was deprecated. There is a possibility that these deprecation warnings are shown even if you don't make use of that feature. You can now disable the feature entirely. -- cgit v1.2.3