Skip to content

Instantly share code, notes, and snippets.

@heaven
Last active March 9, 2016 06:44
Show Gist options
  • Save heaven/55416bc2d2d51dfdb511 to your computer and use it in GitHub Desktop.
Save heaven/55416bc2d2d51dfdb511 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'
# Activate the gem you are reporting the issue against.
gem 'activerecord', :git => 'https://github.com/rails/rails',
:branch => '4-2-stable', :ref => '2aa27582c202148296bb169159b0bf9a47a7bd80'
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 :list_items, force: true do |t|
t.string :type
t.string :name
end
create_table :list_item_relationships, force: true do |t|
t.integer :list_item_id
t.string :list_item_type
t.integer :listable_id
t.string :listable_type
end
create_table :stress_tests, force: true do |t|
end
end
class ListItem < ActiveRecord::Base;
end
class Feeling < ListItem;
end
class ListItemRelationship < ActiveRecord::Base
belongs_to :list_item, :polymorphic => true
belongs_to :listable, :polymorphic => true
validates :list_item_id, :uniqueness => { :scope => [:list_item_type, :listable_id, :listable_type] }
end
class StressTest < ActiveRecord::Base
has_many :list_item_relationships, :as => :listable, :dependent => :destroy
has_many :feelings, :through => :list_item_relationships, :source => :list_item, :source_type => "Feeling"
end
class BugTest < Minitest::Test
def test_has_many_through
feeling = Feeling.create!(:name => "Feeling lucky")
test = StressTest.create!
test.feelings << feeling
assert_equal 1, test.list_item_relationships.count
assert_equal 1, test.feelings.size
assert_equal 1, test.feelings.count
test.feeling_ids = []
assert_equal 0, test.feelings.size
assert_equal 0, test.feelings.count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment