Created
December 10, 2019 10:31
-
-
Save tejasbubane/ff0a7d862a7fe600665349c95882c316 to your computer and use it in GitHub Desktop.
Rails single-table inheritance issue reproduction
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
# 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.0.0" | |
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.string :name | |
end | |
create_table :taxonomies, force: true do |t| | |
t.string :type | |
t.string :post_id | |
end | |
end | |
class ApplicationRecord < ActiveRecord::Base | |
self.abstract_class = true | |
end | |
class Post < ApplicationRecord | |
has_many :taxonomies, dependent: :destroy | |
accepts_nested_attributes_for :taxonomies | |
end | |
class Taxonomy < ApplicationRecord | |
belongs_to :post | |
validates :type, presence: true | |
end | |
class Category < Taxonomy; end | |
class Tag < Taxonomy; end | |
class BugTest < Minitest::Test | |
def test_association_stuff | |
post = Post.create! | |
post.taxonomies << Category.create! | |
assert_equal 1, post.taxonomies.count | |
assert_equal 1, Category.count | |
assert_equal post.id, Category.first.post.id | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment