Skip to content

Instantly share code, notes, and snippets.

@amalagaura
Created December 15, 2015 17:53
Show Gist options
  • Save amalagaura/e426722f80dc4a0ec690 to your computer and use it in GitHub Desktop.
Save amalagaura/e426722f80dc4a0ec690 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'
# Rails 4.2 throws an exception on last test case
gem 'activerecord', '~> 4.2.5'
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_create
category = Category.create(name: '50000000000000')
assert_nil category.posts_count
Post.create!(category_id: category.id)
assert_equal 1, category.reload.posts_count
assert_nil category.category_named_posts_count
Post.create!(category_name: category.name)
assert_equal 1, category.reload.category_named_posts_count
end
def test_string_key_with_counter_cache_on_association_create
# An unnecessary reload required, unlike the integer based association
category = Category.create(name: '50000000000001')
assert_nil category.posts_count
category.posts.create!
assert_equal 1, category.posts_count
assert_nil category.category_named_posts_count
post = category.category_named_posts.create!
assert_equal post.category_name, category.name
# Reload was not required above, but is required for this association
assert_equal 1, category.category_named_posts_count
# This will work in 4.2
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
# On 4.2 this throws an exception when using the category_name to lookup on the primary_key and the name is out of range for integer
# Different behavior than create. Tries to use category_name to lookup category by id
post.save
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