Skip to content

Instantly share code, notes, and snippets.

@jturkel
Created June 10, 2021 14:42
Show Gist options
  • Save jturkel/6e4aa67dfe1d521616806d0b6d42dcec to your computer and use it in GitHub Desktop.
Save jturkel/6e4aa67dfe1d521616806d0b6d42dcec to your computer and use it in GitHub Desktop.
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', '~> 6.0.3'
gem 'goldiloader'
gem 'sqlite3'
gem 'minitest'
end
require 'active_record'
require 'minitest/autorun'
require 'logger'
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :countries, force: true do |t|
end
create_table :people, force: true do |t|
t.string :type
end
create_table :persons_nationalities, force: true do |t|
t.integer :person_id
t.string :person_type
t.integer :nationality_id
end
end
class Person < ActiveRecord::Base
has_many :persons_nationalities, dependent: :destroy, inverse_of: :person, as: :person
end
class Candidate < Person
end
class PersonsNationality < ActiveRecord::Base
belongs_to :person, polymorphic: true
belongs_to :nationality, class_name: "Country"
end
class Country < ActiveRecord::Base
has_many :persons_nationalities, foreign_key: :nationality_id
end
class BugTest < defined?(Minitest::Test) ? Minitest::Test : MiniTest::Unit::TestCase
def test_issue109
candidate = Candidate.create!
country1 = Country.create!
country2 = Country.create!
candidate.persons_nationalities.create!(nationality: country1)
candidate.persons_nationalities.create!(nationality: country2)
found_candiate = Candidate.find(candidate.id)
found_candiate.persons_nationalities[0].nationality
found_candiate.persons_nationalities.each do |persons_nationality|
assert(persons_nationality.association(:nationality).loaded?)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment