Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Reproduction script for rails/rails #27666
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 "http://rubygems.org"
# Activate the gem you are reporting the issue against.
gem "activerecord", "5.0.1"
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 :users, force: true do |t|
end
create_table :families, force: true do |t|
end
create_table :family_trees, force: true do |t|
t.integer :family_id
t.integer :member_id
t.string :token
end
end
class User < ActiveRecord::Base
has_one :family_tree, -> { where(token: nil) }, foreign_key: 'member_id'
has_one :family, through: :family_tree
has_many :family_members, through: :family, source: 'members'
end
class Family < ActiveRecord::Base
has_many :family_trees, -> { where(token: nil) }
has_many :members, through: :family_trees
end
class FamilyTree < ActiveRecord::Base
belongs_to :member, class_name: 'User', foreign_key: 'member_id'
belongs_to :family, class_name: 'Family'
validates :member, uniqueness: { scope: :family }
end
class BugTest < Minitest::Test
def test_association_stuff
family = Family.create!
other_family = Family.create!
users = 4.times.map { User.create! }
FamilyTree.create!(member: users[0], family: family)
FamilyTree.create!(member: users[1], family: family)
FamilyTree.create!(member: users[2], family: family, token: 'wat')
FamilyTree.create!(member: users[3], family: other_family)
assert_equal 2, users[0].family_members.to_a.size
assert_equal 0, users[2].family_members.to_a.size
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.