Last active
November 26, 2016 12:45
-
-
Save utilum/5296f9e34ee8bcfaf8a5841164326968 to your computer and use it in GitHub Desktop.
decrement for counter_cache not updated in 4.2.7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "rails", github: "rails/rails", tag: "v4.2.7.1" | |
gem "sqlite3" | |
end | |
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, force: true do |t| | |
t.integer :nested_comments_count | |
t.integer :un_nested_comments_count | |
t.string :title | |
end | |
create_table :nested_comments, force: true do |t| | |
t.integer :post_id | |
t.string :title | |
end | |
create_table :un_nested_comments, force: true do |t| | |
t.integer :post_id | |
t.string :title | |
end | |
end | |
class Post < ActiveRecord::Base | |
has_many :nested_comments, inverse_of: :post | |
has_many :un_nested_comments, inverse_of: :post | |
accepts_nested_attributes_for :nested_comments, allow_destroy: true | |
end | |
class NestedComment < ActiveRecord::Base | |
belongs_to :post, inverse_of: :nested_comments, counter_cache: true | |
end | |
class UnNestedComment < ActiveRecord::Base | |
belongs_to :post, inverse_of: :un_nested_comments, counter_cache: true | |
end | |
class BugTest < Minitest::Test | |
def setup | |
Post.delete_all | |
NestedComment.delete_all | |
UnNestedComment.delete_all | |
end | |
def test_decreases_nested_associations_count_after_destroy | |
post = Post.create( | |
title: "Test Title", | |
nested_comments_attributes: { | |
0 => { title: ' NestedComment 1' }, | |
}, | |
) | |
assert_equal 1, post.nested_comments_count | |
assert_equal 1, post.nested_comments.count | |
assert_equal 1, NestedComment.count | |
assert_equal post.id, NestedComment.first.post.id | |
post.nested_comments.first.destroy | |
assert_equal 0, post.nested_comments.count | |
assert_equal 0, post.nested_comments_count | |
end | |
def test_decreases_nested_associations_count_after_delete | |
post = Post.create( | |
title: "Test Title", | |
nested_comments_attributes: { | |
0 => { title: ' NestedComment 1' }, | |
}, | |
) | |
post.nested_comments.last.delete | |
assert_equal 0, post.nested_comments.count | |
assert_equal 1, post.nested_comments_count | |
post.title = "Change Test Title" | |
post.save | |
assert_equal 0, post.nested_comments_count | |
end | |
def test_decreases_un_nested_associations_count_after_destroy | |
post = Post.create(title: "Test Title") | |
UnNestedComment.create(post: post, title: ' UnNestedComment 1') | |
assert_equal 1, post.un_nested_comments_count | |
assert_equal 1, post.un_nested_comments.count | |
assert_equal 1, UnNestedComment.count | |
assert_equal post.id, UnNestedComment.first.post.id | |
post.un_nested_comments.first.destroy | |
assert_equal 0, post.un_nested_comments.count | |
assert_equal 0, post.un_nested_comments_count | |
end | |
def test_decreases_un_nested_associations_count_after_delete | |
post = Post.create(title: "Test Title") | |
UnNestedComment.create(post: post, title: ' UnNestedComment 1') | |
post.un_nested_comments.last.delete | |
assert_equal 0, post.un_nested_comments.count | |
assert_equal 1, post.un_nested_comments_count | |
post.title = "Change Test Title" | |
post.save | |
assert_equal 0, post.un_nested_comments_count | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment