Created
July 20, 2018 19:03
-
-
Save stas/7a9a6cbf76ffe8330059dfda0b5fa57a to your computer and use it in GitHub Desktop.
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 "bundler/inline" | |
gemfile(true) do | |
source "https://rubygems.org" | |
gem "activerecord", "~> 4" | |
gem "pg", "0.19" | |
end | |
require "active_record" | |
require "minitest/autorun" | |
require "logger" | |
# Ensure backward compatibility with Minitest 4 | |
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test) | |
# This connection will do for database-independent bug reports. | |
ActiveRecord::Base.establish_connection(ENV['DATABASE_URL']) | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
create_table :books, force: true do |t| | |
t.string :title | |
end | |
create_table :tags, force: true do |t| | |
t.string :title | |
end | |
create_table :taggings, force: true do |t| | |
t.references :taggable, polymorphic: true, type: :string | |
t.references :tag | |
end | |
end | |
class Tagging < ActiveRecord::Base | |
belongs_to :tag | |
belongs_to :taggable, polymorphic: true | |
end | |
class Book < ActiveRecord::Base | |
has_many :taggings, as: :taggable | |
end | |
class Tag < ActiveRecord::Base | |
has_many :taggings | |
has_many :books, through: :taggings, source: :taggable, source_type: Book.name | |
end | |
class BugTest < Minitest::Test | |
def test_association_stuff | |
book = Book.create!(title: 'Book') | |
tag = Tag.create!(title: 'Tag') | |
tagging = Tagging.create!(taggable: book, tag: tag) | |
assert_equal 1, tag.taggings.count | |
assert_equal 1, tag.books.count | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The output: