Skip to content

Instantly share code, notes, and snippets.

@doits
Created December 28, 2020 17:18
Show Gist options
  • Save doits/90442715bd59eb754556219ced6ff480 to your computer and use it in GitHub Desktop.
Save doits/90442715bd59eb754556219ced6ff480 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# Activate the gem you are reporting the issue against.
gem "activerecord", "6.1.0"
gem "activesupport", "6.1.0"
gem "sqlite3"
end
require "active_record"
require "minitest/autorun"
require "logger"
require 'active_support/testing/assertions'
ActiveRecord::Base.has_many_inversing = true
# 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 :pages, force: true do |t|
t.string :name
t.bigint :parent_id, foreign_key: true
t.timestamps
end
create_table :page_comments do |t|
t.string :name
t.references :page, null: false, foreign_key: true
end
end
class Page < ActiveRecord::Base
belongs_to :parent, class_name: 'Page', foreign_key: 'parent_id', optional: true, touch: true, inverse_of: :children
has_many :children, class_name: 'Page', foreign_key: 'parent_id', inverse_of: :parent
has_many :comments, class_name: 'PageComment', inverse_of: :page
end
class PageComment < ActiveRecord::Base
belongs_to :page, touch: true, inverse_of: :comments
end
class BugTest < Minitest::Test
include ActiveSupport::Testing::Assertions
def test_association_stuff
root= Page.create!(name: 'parent')
child= root.children.create!(name: 'child')
child.reload
assert_difference 'Page.count', -1 do
child.destroy
end
end
def test_association_stuff2
root= Page.create!(name: 'parent')
child= root.comments.create!(name: 'child')
child.reload
assert_difference 'PageComment.count', -1 do
child.destroy
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment