Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

AR counter cache test

View gist:7940834
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
GEMFILE
 
system 'bundle'
end
 
require 'bundler'
Bundler.setup(:default)
 
require 'active_record'
require 'minitest/autorun'
require 'logger'
 
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
 
ActiveRecord::Schema.define do
create_table :posts do |t|
t.integer :comments_count
end
 
create_table :comments do |t|
t.integer :post_id
end
end
 
class Post < ActiveRecord::Base
has_many :comments
end
 
class Comment < ActiveRecord::Base
belongs_to :post, counter_cache: true
end
 
class BugTest < Minitest::Test
def test_counter_cache
post = Post.create!
comment = Comment.create!
 
comment.post = post
comment.save
assert_equal 1, post.reload.comments_count
# 1) Failure:
# BugTest#test_counter_cache [counter_cache.rb:48]:
# Expected: 1
# Actual: 2
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.