aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2012-10-02 09:23:02 -0700
committerRafael Mendonça França <rafaelmfranca@gmail.com>2012-10-02 15:58:49 -0300
commit3f0bc97912482c2da9e70683b029feb36de74c27 (patch)
tree68c9326af7dbba14f3978f2075ded7ad4ea33ade /activerecord/test
parent2bad605873b5b720d77ae6388a995827ab7fe705 (diff)
downloadrails-3f0bc97912482c2da9e70683b029feb36de74c27.tar.gz
rails-3f0bc97912482c2da9e70683b029feb36de74c27.tar.bz2
rails-3f0bc97912482c2da9e70683b029feb36de74c27.zip
Merge pull request #7822 from lulalala/reset-counter-cache-for-has-many-through
Fix reset_counters crashing on has_many :through associations. Conflicts: activerecord/CHANGELOG.md activerecord/lib/active_record/counter_cache.rb
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/counter_cache_test.rb15
-rw-r--r--activerecord/test/models/subscription.rb2
-rw-r--r--activerecord/test/schema/schema.rb1
3 files changed, 16 insertions, 2 deletions
diff --git a/activerecord/test/cases/counter_cache_test.rb b/activerecord/test/cases/counter_cache_test.rb
index ee443741ca..fc46a249c8 100644
--- a/activerecord/test/cases/counter_cache_test.rb
+++ b/activerecord/test/cases/counter_cache_test.rb
@@ -10,9 +10,12 @@ require 'models/dog'
require 'models/dog_lover'
require 'models/person'
require 'models/friendship'
+require 'models/subscriber'
+require 'models/subscription'
+require 'models/book'
class CounterCacheTest < ActiveRecord::TestCase
- fixtures :topics, :categories, :categorizations, :cars, :dogs, :dog_lovers, :people, :friendships
+ fixtures :topics, :categories, :categorizations, :cars, :dogs, :dog_lovers, :people, :friendships, :subscribers, :subscriptions, :books
class ::SpecialTopic < ::Topic
has_many :special_replies, :foreign_key => 'parent_id'
@@ -118,4 +121,14 @@ class CounterCacheTest < ActiveRecord::TestCase
Person.reset_counters(michael.id, :followers)
end
end
+
+ test "reset counter of has_many :through association" do
+ subscriber = subscribers('second')
+ Subscriber.reset_counters(subscriber.id, 'books')
+ Subscriber.increment_counter('books_count', subscriber.id)
+
+ assert_difference 'subscriber.reload.books_count', -1 do
+ Subscriber.reset_counters(subscriber.id, 'books')
+ end
+ end
end
diff --git a/activerecord/test/models/subscription.rb b/activerecord/test/models/subscription.rb
index 4bdb36ea46..bcac4738a3 100644
--- a/activerecord/test/models/subscription.rb
+++ b/activerecord/test/models/subscription.rb
@@ -1,4 +1,4 @@
class Subscription < ActiveRecord::Base
- belongs_to :subscriber
+ belongs_to :subscriber, :counter_cache => :books_count
belongs_to :book
end
diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb
index b8150c50b9..8a3dfbb35a 100644
--- a/activerecord/test/schema/schema.rb
+++ b/activerecord/test/schema/schema.rb
@@ -596,6 +596,7 @@ ActiveRecord::Schema.define do
create_table :subscribers, :force => true, :id => false do |t|
t.string :nick, :null => false
t.string :name
+ t.column :books_count, :integer, :null => false, :default => 0
end
add_index :subscribers, :nick, :unique => true