From 615d15a37ab7dbeaa1e246af0a86e28c3ba63106 Mon Sep 17 00:00:00 2001 From: Balint Erdi Date: Fri, 15 Jan 2010 13:32:06 +0100 Subject: [PATCH] association collection size returns correct value through custom cache_counter --- .../associations/has_many_association.rb | 17 +++++++++++------ .../associations/belongs_to_associations_test.rb | 4 ++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 4e113be..0ea5015 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -40,11 +40,11 @@ module ActiveRecord # we are certain the current target is an empty array. This is a # documented side-effect of the method that may avoid an extra SELECT. @target ||= [] and loaded if count == 0 - + if @reflection.options[:limit] count = [ @reflection.options[:limit], count ].min end - + return count end @@ -53,7 +53,12 @@ module ActiveRecord end def cached_counter_attribute_name - "#{@reflection.name}_count" + ( belongs_to_association_in_target && belongs_to_association_in_target.counter_cache_column ) || "#{@reflection.name}_count" + end + + def belongs_to_association_in_target + target_belongs_to_associations = @reflection.klass.reflect_on_all_associations.select(&:belongs_to?) + target_belongs_to_associations.detect { |a| a.class_name == @owner.class.to_s } end def insert_record(record, force = false, validate = true) @@ -71,7 +76,7 @@ module ActiveRecord else ids = quoted_record_ids(records) @reflection.klass.update_all( - "#{@reflection.primary_key_name} = NULL", + "#{@reflection.primary_key_name} = NULL", "#{@reflection.primary_key_name} = #{owner_quoted_id} AND #{@reflection.klass.primary_key} IN (#{ids})" ) @owner.class.update_counters(@owner.id, cached_counter_attribute_name => -records.size) if has_cached_counter? @@ -88,11 +93,11 @@ module ActiveRecord @finder_sql = interpolate_sql(@reflection.options[:finder_sql]) when @reflection.options[:as] - @finder_sql = + @finder_sql = "#{@reflection.quoted_table_name}.#{@reflection.options[:as]}_id = #{owner_quoted_id} AND " + "#{@reflection.quoted_table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.base_class.name.to_s)}" @finder_sql << " AND (#{conditions})" if conditions - + else @finder_sql = "#{@reflection.quoted_table_name}.#{@reflection.primary_key_name} = #{owner_quoted_id}" @finder_sql << " AND (#{conditions})" if conditions diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index 5769066..868acf4 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -307,7 +307,7 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase def test_custom_counter_cache reply = Reply.create(:title => "re: zoom", :content => "speedy quick!") - assert_equal 0, reply[:replies_count] + assert_equal 0, reply[:inane_replies_count] silly = SillyReply.create(:title => "gaga", :content => "boo-boo") silly.reply = reply @@ -315,7 +315,7 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase assert_equal 1, reply.reload[:inane_replies_count] assert_equal 1, reply.replies.size - reply[:replies_count] = 17 + reply[:inane_replies_count] = 17 assert_equal 17, reply.replies.size end -- 1.6.5