diff options
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/association_proxy.rb | 8 | ||||
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 18 | ||||
-rw-r--r-- | activerecord/test/cases/associations/has_many_associations_test.rb | 8 | ||||
-rwxr-xr-x | activerecord/test/cases/base_test.rb | 10 |
5 files changed, 40 insertions, 6 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index d8bfb1916d..d67b61dc07 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *Edge* +* Reset your Active Record counter caches with the reset_counter_cache class method. #1211 [Mike Breen] + * Remove support for SQLite 2. Please upgrade to SQLite 3+ or install the plugin from git://github.com/rails/sqlite2_adapter.git [Pratik Naik] * PostgreSQL: XML datatype support. #1874 [Leonardo Borges] 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 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 diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 86d14c9c81..3c490c1eeb 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -1178,5 +1178,13 @@ class HasManyAssociationsTest < ActiveRecord::TestCase client = firm.clients_using_primary_key.create!(:name => 'test') assert_equal firm.name, client.firm_name end + + def test_normal_method_call_in_association_proxy + assert_equal 'Welcome to the weblog', Comment.all.map { |comment| comment.post }.first.title + end + + def test_instance_eval_in_association_proxy + assert_equal 'Welcome to the weblog', Comment.all.map { |comment| comment.post }.first.instance_eval{title} + end end diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 5c2911eca1..737ca01d46 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -680,6 +680,16 @@ class BasicsTest < ActiveRecord::TestCase 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 |