aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorMike Breen <hardbap@gmail.com>2009-05-20 10:31:12 -0400
committerJeremy Kemper <jeremy@bitsweat.net>2009-12-02 12:20:00 -0800
commit50c28e78c7aa40dc329facbe6131d657d5629bd4 (patch)
tree85892682a5ef70729f11ec5742d25634f4f56351 /activerecord/lib/active_record
parentde2cd8e39f68d874b833b3a4860769c4edc57cbe (diff)
downloadrails-50c28e78c7aa40dc329facbe6131d657d5629bd4.tar.gz
rails-50c28e78c7aa40dc329facbe6131d657d5629bd4.tar.bz2
rails-50c28e78c7aa40dc329facbe6131d657d5629bd4.zip
Implement ActiveRecord#reset_counter_cache
[#1211 state:committed] Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Diffstat (limited to 'activerecord/lib/active_record')
-rwxr-xr-xactiverecord/lib/active_record/base.rb18
1 files changed, 18 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 056f29f029..e04684dc43 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -967,6 +967,24 @@ module ActiveRecord #:nodoc:
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