Skip to content

Instantly share code, notes, and snippets.

@amalagaura
Created December 15, 2015 17:52
Show Gist options
  • Save amalagaura/7de9a6716748b818e8f6 to your computer and use it in GitHub Desktop.
Save amalagaura/7de9a6716748b818e8f6 to your computer and use it in GitHub Desktop.
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'activerecord', '~> 4.1.14'
gem 'sqlite3'
end
require 'active_record'
require 'minitest/autorun'
require 'logger'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# 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 :categories, force: true do |t|
t.string :name, null: false
t.integer :posts_count
t.integer :category_named_posts_count
end
create_table :Posts, force: true do |t|
t.integer :category_id
t.string :category_name
end
end
class Category < ActiveRecord::Base
has_many :posts
has_many :category_named_posts, foreign_key: :category_name, primary_key: :name, class_name: 'Post'
end
class Post < ActiveRecord::Base
belongs_to :category, counter_cache: true
belongs_to :named_category, :class_name => 'Category', :foreign_key => :category_name, :primary_key => :name, :counter_cache => :category_named_posts_count
end
class BugTest < Minitest::Test
def test_string_key_with_counter_cache_on_association_create
category = Category.create(name: '50000000000001')
assert_nil category.posts_count
category.posts.create!
# Fails without reload
# assert_equal 1, category.posts_count
# Reload required, below will work
assert_equal 1, category.reload.posts_count
assert_nil category.category_named_posts_count
post = category.category_named_posts.create!
assert_equal post.category_name, category.name
# Fails without reload
# assert_equal 1, category.category_named_posts_count
# Reload required, below will work
assert_equal 1, category.reload.category_named_posts_count
end
def test_string_key_with_counter_cache_on_association_save
category = Category.create(name: '50000000000002')
# This works
assert_nil category.posts_count
post = Post.create
post.category_id = category.id
post.save!
assert_equal 1, category.reload.posts_count
assert_nil category.category_named_posts_count
post = Post.create
post.category_name = category.name
post.save
# Broken just like 4.2, except that the range check is not performed. Should search on category.category_name, not category.id
# UPDATE "categories" SET "category_named_posts_count" = COALESCE("category_named_posts_count", 0) + 1 WHERE "categories"."id" = 50000000000002
assert_equal 1, category.reload.category_named_posts_count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment