Skip to content

Instantly share code, notes, and snippets.

@jturkel
Created April 10, 2016 01:37
Show Gist options
  • Select an option

  • Save jturkel/19f8b26e04a3a6e462d4f4a17db4be51 to your computer and use it in GitHub Desktop.

Select an option

Save jturkel/19f8b26e04a3a6e462d4f4a17db4be51 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 'activerecord', '4.2.4'
gem 'goldiloader'
gem 'sqlite3'
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(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :subclasses, force: true do |t|
t.string :type
end
create_table :participants, force: true do |t|
end
create_table :participants_subclasses, force: true do |t|
t.integer :subclass_id
t.integer :participant_id
end
end
class Subclass < ActiveRecord::Base
end
class ParticipantSubclass < Subclass
end
class Participant < ActiveRecord::Base
has_and_belongs_to_many :participant_subclasses,
association_foreign_key: :subclass_id
end
class BugTest < Minitest::Test
def test_habtm
participant = Participant.create!
participant_subclasses = 3.times.map { ParticipantSubclass.create! }
participant_subclasses.each do |participant_subclass|
participant.participant_subclasses << participant_subclass
end
found_participant_subclasses = Participant.find(participant.id).participant_subclasses.to_a
assert_equal(participant_subclasses.sort_by(&:id), found_participant_subclasses.sort_by(&:id))
assert_equal(3, found_participant_subclasses.size)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment