Skip to content

Commit

Permalink
Merge pull request rails#5210 from Pliny/masteri
Browse files Browse the repository at this point in the history
Fix for rails#5200

Conflicts:
	activerecord/CHANGELOG.md
	activerecord/lib/active_record/counter_cache.rb
  • Loading branch information
rafaelfranca committed Aug 21, 2012
1 parent c091fae commit 648c5a1
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 2 deletions.
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,5 +1,11 @@
## Rails 3.2.9 (unreleased)

* Fix `reset_counters` when there are multiple `belongs_to` association with the
same foreign key and one of them have a counter cache.
Fixes #5200.

*Dave Desrochers*

* Round usec when comparing timestamp attributes in the dirty tracking.
Fixes #6975.

Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/counter_cache.rb
Expand Up @@ -28,7 +28,7 @@ def reset_counters(id, *counters)
foreign_key = has_many_association.foreign_key.to_s
child_class = has_many_association.klass
belongs_to = child_class.reflect_on_all_associations(:belongs_to)
reflection = belongs_to.find { |e| e.foreign_key.to_s == foreign_key }
reflection = belongs_to.find { |e| e.foreign_key.to_s == foreign_key && e.options[:counter_cache].present? }
counter_name = reflection.counter_cache_column

stmt = unscoped.where(arel_table[primary_key].eq(object.id)).arel.compile_update({
Expand Down
11 changes: 10 additions & 1 deletion activerecord/test/cases/counter_cache_test.rb
Expand Up @@ -8,9 +8,11 @@
require 'models/categorization'
require 'models/dog'
require 'models/dog_lover'
require 'models/person'
require 'models/friendship'

class CounterCacheTest < ActiveRecord::TestCase
fixtures :topics, :categories, :categorizations, :cars, :dogs, :dog_lovers
fixtures :topics, :categories, :categorizations, :cars, :dogs, :dog_lovers, :people, :friendships

class ::SpecialTopic < ::Topic
has_many :special_replies, :foreign_key => 'parent_id'
Expand Down Expand Up @@ -109,4 +111,11 @@ class ::SpecialReply < ::Reply
Topic.update_counters([t1.id, t2.id], :replies_count => 2)
end
end

test "reset the right counter if two have the same foreign key" do
michael = people(:michael)
assert_nothing_raised(ActiveRecord::StatementInvalid) do
Person.reset_counters(michael.id, :followers)
end
end
end
4 changes: 4 additions & 0 deletions activerecord/test/fixtures/friendships.yml
@@ -0,0 +1,4 @@
Connection 1:
id: 1
person_id: 1
friend_id: 2
3 changes: 3 additions & 0 deletions activerecord/test/fixtures/people.yml
Expand Up @@ -4,15 +4,18 @@ michael:
primary_contact_id: 2
number1_fan_id: 3
gender: M
followers_count: 1
david:
id: 2
first_name: David
primary_contact_id: 3
number1_fan_id: 1
gender: M
followers_count: 1
susan:
id: 3
first_name: Susan
primary_contact_id: 2
number1_fan_id: 1
gender: F
followers_count: 1
4 changes: 4 additions & 0 deletions activerecord/test/models/friendship.rb
@@ -0,0 +1,4 @@
class Friendship < ActiveRecord::Base
belongs_to :friend, class_name: 'Person'
belongs_to :follower, foreign_key: 'friend_id', class_name: 'Person', counter_cache: :followers_count
end
2 changes: 2 additions & 0 deletions activerecord/test/models/person.rb
Expand Up @@ -7,6 +7,8 @@ class Person < ActiveRecord::Base
has_many :secure_posts, :through => :secure_readers
has_many :posts_with_no_comments, :through => :readers, :source => :post, :include => :comments, :conditions => 'comments.id is null'

has_many :followers, foreign_key: 'friend_id', class_name: 'Friendship'

has_many :references
has_many :bad_references
has_many :fixed_bad_references, :conditions => { :favourite => true }, :class_name => 'BadReference'
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/schema/schema.rb
Expand Up @@ -265,6 +265,11 @@ def create_table(*args, &block)
t.string :name
end

create_table :friendships, :force => true do |t|
t.integer :friend_id
t.integer :person_id
end

create_table :goofy_string_id, :force => true, :id => false do |t|
t.string :id, :null => false
t.string :info
Expand Down Expand Up @@ -465,6 +470,7 @@ def create_table(*args, &block)
t.references :number1_fan
t.integer :lock_version, :null => false, :default => 0
t.string :comments
t.integer :followers_count, :default => 0
t.references :best_friend
t.references :best_friend_of
t.timestamps
Expand Down

0 comments on commit 648c5a1

Please sign in to comment.