Skip to content

Commit

Permalink
Replace reset_counter_cache with reset_counters that has API inline w…
Browse files Browse the repository at this point in the history
…ith existing update_counters method

[#1211 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information
gtd authored and jeremy committed Dec 4, 2009
1 parent ccb197b commit 43d2cb8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion activerecord/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

* MySQL: add_ and change_column support positioning. #3286 [Ben Marini]

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


*2.3.5 (November 25, 2009)*
Expand Down
21 changes: 13 additions & 8 deletions activerecord/lib/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -916,20 +916,25 @@ def count_by_sql(sql)
connection.select_value(sql, "#{name} Count").to_i
end

# Reset a counter cache for all records.
# Resets one or more counter caches to their correct value using an SQL
# count query. This is useful when adding new counter caches, or if the
# counter has been corrupted or modified directly by SQL.
#
# ==== Parameters
#
# * +association_name+ - The name of of the association counter cache to reset
# * +id+ - The id of the object you wish to reset a counter on.
# * +counters+ - One or more counter names 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
#
# # For Post with id #1 records reset the comments_count
# Post.reset_counters(1, :comments)
def reset_counters(id, *counters)
object = find(id)
counters.each do |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
Expand Down
4 changes: 2 additions & 2 deletions activerecord/test/cases/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -627,13 +627,13 @@ def test_decrement_counter
assert_equal -2, Topic.find(2).replies_count
end

def test_reset_counter_cache
def test_reset_counters
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)
Topic.reset_counters(1, :replies)
assert_equal 1, Topic.find(1).replies_count
end

Expand Down

1 comment on commit 43d2cb8

@granth
Copy link
Contributor

@granth granth commented on 43d2cb8 Dec 4, 2009

Choose a reason for hiding this comment

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

The method name should be changed to reset_counters here, too.

Please sign in to comment.