Last active
January 1, 2016 04:28
-
-
Save faun/8091701 to your computer and use it in GitHub Desktop.
Outdated gist related to https://github.com/rails/rails/issues/13304. Move along.
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
require 'rubygems' | |
require 'sqlite3' | |
require 'active_record' | |
require 'minitest/autorun' | |
ActiveRecord::Base.establish_connection 'sqlite3:///:memory:' | |
ActiveRecord::Schema.define do | |
create_table(:posts) do |t| | |
t.integer :author_count, default: 0 | |
end | |
create_table(:authors) | |
create_table(:authorships) do |t| | |
t.integer :author_id | |
t.integer :post_id | |
end | |
end | |
# Post has many Authors through Authorships | |
class Post < ActiveRecord::Base | |
has_many :authorships | |
has_many :authors, | |
through: :authorships | |
end | |
# Joining model between Author <=> Post | |
class Authorship < ActiveRecord::Base | |
belongs_to :author | |
belongs_to :post, counter_cache: :author_count | |
end | |
# Author has many Posts | |
class Author < ActiveRecord::Base | |
has_many :authorships | |
has_many :posts, through: :authorships | |
end | |
# Replicate the counter_cache decriment issue when removing an Author from Post | |
class BugTest < Minitest::Test | |
def test_counter_cache | |
post = Post.create! | |
author_1 = Author.create! | |
author_2 = Author.create! | |
assert_equal 0, post.author_count | |
post.authors << author_1 | |
assert_equal 1, post.reload.author_count | |
post.authors << author_2 | |
assert_equal 2, post.reload.author_count | |
post.authors.destroy(author_2) | |
assert_equal 1, post.reload.author_count | |
end | |
end |
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
source 'https://rubygems.org' | |
gem 'activerecord', github: 'rails/rails' | |
gem 'sqlite3' | |
gem 'minitest', '~> 5.1' |
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
GIT | |
remote: git://github.com/rails/rails.git | |
revision: 1a6089f30565cc3fd19c02c2331fdd255ff23502 | |
specs: | |
activemodel (4.1.0.beta1) | |
activesupport (= 4.1.0.beta1) | |
builder (~> 3.1) | |
activerecord (4.1.0.beta1) | |
activemodel (= 4.1.0.beta1) | |
activesupport (= 4.1.0.beta1) | |
arel (~> 5.0.0) | |
activesupport (4.1.0.beta1) | |
i18n (~> 0.6, >= 0.6.9) | |
json (~> 1.7, >= 1.7.7) | |
minitest (~> 5.1) | |
thread_safe (~> 0.1) | |
tzinfo (~> 1.1) | |
GEM | |
remote: https://rubygems.org/ | |
specs: | |
arel (5.0.0) | |
atomic (1.1.14) | |
builder (3.2.2) | |
i18n (0.6.9) | |
json (1.8.1) | |
minitest (5.2.0) | |
sqlite3 (1.3.8) | |
thread_safe (0.1.3) | |
atomic | |
tzinfo (1.1.0) | |
thread_safe (~> 0.1) | |
PLATFORMS | |
ruby | |
DEPENDENCIES | |
activerecord! | |
minitest (~> 5.1) | |
sqlite3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-- create_table(:posts)
-> 0.0208s
-- create_table(:authors)
-> 0.0003s
-- create_table(:authorships)
-> 0.0004s
Run options: --seed 65434
Running:
.
Finished in 0.024955s, 40.0721 runs/s, 160.2885 assertions/s.
1 runs, 4 assertions, 0 failures, 0 errors, 0 skips