From 50c28e78c7aa40dc329facbe6131d657d5629bd4 Mon Sep 17 00:00:00 2001 From: Mike Breen Date: Wed, 20 May 2009 10:31:12 -0400 Subject: Implement ActiveRecord#reset_counter_cache [#1211 state:committed] Signed-off-by: Jeremy Kemper --- activerecord/lib/active_record/base.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'activerecord/lib') 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 -- cgit v1.2.3 From 49e943c4f0ac3459bd53023167aaa08fc8e46733 Mon Sep 17 00:00:00 2001 From: Mat Brown Date: Thu, 22 Oct 2009 10:20:44 -0400 Subject: Fix instance_eval calls to association proxies In the current stable, ActiveRecord::Associations::AssociationProxy#method_missing calls yield() if a block is given, causing the block to always be evaluated in its calling context. However, in the case of instance_eval, correct behavior requires that the block be passed directly to the @target, rather than being evaluated inside a different block. Incidentally, this also simplifies the code slightly. [#3412 state:committed] Signed-off-by: Jeremy Kemper --- activerecord/lib/active_record/associations/association_proxy.rb | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb index 7d8f4670fa..6ad1e06300 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -200,18 +200,14 @@ module ActiveRecord private # Forwards any missing method call to the \target. - def method_missing(method, *args) + def method_missing(method, *args, &block) if load_target unless @target.respond_to?(method) message = "undefined method `#{method.to_s}' for \"#{@target}\":#{@target.class.to_s}" raise NoMethodError, message end - if block_given? - @target.send(method, *args) { |*block_args| yield(*block_args) } - else - @target.send(method, *args) - end + @target.send(method, *args, &block) end end -- cgit v1.2.3