Skip to content

Commit

Permalink
Implement ActiveRecord#reset_counter_cache
Browse files Browse the repository at this point in the history
[#1211 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information
hardbap authored and jeremy committed Dec 2, 2009
1 parent da61a6c commit 1db3a27
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG
@@ -1,3 +1,8 @@
*2.3.6 (pending)*

* Reset your Active Record counter caches with the reset_counter_cache class method. #1211 [Mike Breen]


*2.3.5 (November 25, 2009)*

* Minor Bug Fixes and deprecation warnings
Expand All @@ -6,6 +11,7 @@

* Numerous fixes to the nested attributes functionality


*2.3.4 (September 4, 2009)*

* PostgreSQL: XML datatype support. #1874 [Leonardo Borges]
Expand Down
18 changes: 18 additions & 0 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -916,6 +916,24 @@ def count_by_sql(sql)
connection.select_value(sql, "#{name} Count").to_i
end

# Reset a counter cache for all records.
#
# ==== Parameters
#
# * +association_name+ - The name of of the association counter cache to reset
#
# ==== Examples
# # For all Post records reset the comments_count
# Post.reset_counter_cache(:comments)
def reset_counter_cache(association)
child_class = reflect_on_association(association).klass
counter_name = child_class.reflect_on_association(self.name.downcase.to_sym).counter_cache_column

find_each do |object|
connection.update("UPDATE #{quoted_table_name} SET #{connection.quote_column_name(counter_name)} = #{object.send(association).count} WHERE #{connection.quote_column_name(primary_key)} = #{quote_value(object.id)}", "#{name} UPDATE")
end
end

# A generic "counter updater" implementation, intended primarily to be
# used by increment_counter and decrement_counter, but which may also
# be useful on its own. It simply does a direct SQL update for the record
Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/base_test.rb
Expand Up @@ -627,6 +627,16 @@ def test_decrement_counter
assert_equal -2, Topic.find(2).replies_count
end

def test_reset_counter_cache
assert_equal 1, Topic.find(1).replies_count

Topic.increment_counter("replies_count", 1)
assert_equal 2, Topic.find(1).replies_count

Topic.reset_counter_cache(:replies)
assert_equal 1, Topic.find(1).replies_count
end

def test_update_counter
category = categories(:general)
assert_nil category.categorizations_count
Expand Down

1 comment on commit 1db3a27

@gtd
Copy link
Contributor

@gtd gtd commented on 1db3a27 Dec 2, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.