Skip to content

Instantly share code, notes, and snippets.

@utilum
Last active November 30, 2016 23:50
Show Gist options
  • Save utilum/8bc7814985f09555f867c6b528640bd3 to your computer and use it in GitHub Desktop.
Save utilum/8bc7814985f09555f867c6b528640bd3 to your computer and use it in GitHub Desktop.
Test after_destroy is not called on has_many through #27099 on 4.7.2
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", "4.7.2" #github: "rails/rails"
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 do |t|
end
create_table :posts_tags do |t|
t.integer :post_id
t.integer :tag_id
end
create_table :tags do |t|
end
create_table :signs do |t|
end
end
class Sign < ActiveRecord::Base
end
class Post < ActiveRecord::Base
has_many :posts_tags
has_many :tags, through: :posts_tags
end
class PostsTag < ActiveRecord::Base
belongs_to :post
belongs_to :tag
after_destroy :create_sign
after_create :create_sign
private
def create_sign
Sign.create
end
end
class Tag < ActiveRecord::Base
has_many :posts_tags
has_many :posts, through: :posts_tags
end
class BugTest < Minitest::Test
def test_after_destroy_on_has_many_through
post = Post.create!
post.tags << Tag.create!
post.tags << Tag.create!
assert_equal 2, Post.first.tags.count
assert_equal 2, PostsTag.count
assert_equal 2, Tag.count
assert_equal 2, Sign.count
assert_equal post.id, Tag.first.posts.first.id
post.tags.destroy(Tag.first)
assert_equal 1, Post.first.tags.count
assert_equal 1, PostsTag.count
assert_equal 3, Sign.count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment