-
-
Save nachokb/2c9b171ee49055c2260b to your computer and use it in GitHub Desktop.
issue 18061
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
# rails/rails#18061 | |
# env vars: AR_VERSION (see below for default), TRY_PATCH (default false) | |
system 'rm Gemfile' if File.exist?('Gemfile') | |
$ar_version = ENV['AR_VERSION'] | |
# $ar_version ||= '5.0.0.beta3' # succeeds, patch does not apply (lacks #where_values) | |
$ar_version ||= '4.2.6' # fails, patch works | |
# $ar_version ||= '4.1.15' # fails, patch works | |
# $ar_version ||= '4.0.13' # fails, patch works | |
$try_patch = ENV['TRY_PATCH'] == 'true' | |
$enable_byebug = false | |
File.write('Gemfile', <<-GEMFILE) | |
source 'https://rubygems.org' | |
gem 'activerecord', | |
#{$ar_version.inspect} | |
gem 'sqlite3' | |
#{"gem 'byebug'" if $enable_byebug} | |
GEMFILE | |
system 'bundle install' | |
require 'byebug' if $enable_byebug | |
require 'bundler' | |
Bundler.setup(:default) | |
require 'active_record' | |
require 'minitest/autorun' | |
require 'logger' | |
# 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 :businesses do |t| | |
t.string :type | |
t.integer :registry_id | |
end | |
create_table :memberships do |t| | |
t.integer :company_id | |
t.integer :foundation_id | |
t.date :expires_at | |
end | |
create_table :registries do |t| | |
end | |
end | |
class Registry < ActiveRecord::Base | |
has_many :foundations | |
has_many :companies, | |
through: :foundations | |
module TargetScopeFix | |
def association(*args) | |
(args[0] == :companies) ? super.extend(Association) : super | |
end | |
module Association | |
def target_scope | |
super.tap { |scope| | |
scope.where_values.delete_if { |node| | |
left = node.left | |
right = node.right[0] | |
Arel::Nodes::In === node && | |
left.relation.name == 'businesses' && | |
left.name.to_s == 'type' && | |
# #val is for 4.2 | |
(right.respond_to?(:val) ? right.val : right) != 'Company' | |
} | |
} | |
end | |
end | |
end | |
include TargetScopeFix if $try_patch | |
end | |
class Business < ActiveRecord::Base | |
end | |
class Foundation < Business | |
belongs_to :registry | |
has_many :memberships | |
has_many :companies, | |
through: :memberships | |
end | |
class Company < Business | |
has_many :memberships | |
has_many :foundations, | |
through: :memberships | |
end | |
class Membership < ActiveRecord::Base | |
belongs_to :foundation | |
belongs_to :company | |
end | |
# class BugTest < Minitest::Test | |
class BugTest < MiniTest::Unit::TestCase | |
def setup | |
companya = Company.create | |
companyb = Company.create | |
foundationa = Foundation.create | |
foundationb = Foundation.create | |
registry = Registry.create | |
registryb = Registry.create | |
companya.memberships.create foundation: foundationa | |
companya.memberships.create foundation: foundationb | |
companyb.memberships.create foundation: foundationb | |
registry.foundations << foundationa | |
registryb.foundations << foundationb | |
end | |
# generated query: | |
# SELECT "businesses".* | |
# FROM "businesses" | |
# INNER JOIN "memberships" | |
# ON "businesses"."id" = "memberships"."company_id" | |
# INNER JOIN "businesses" "foundations_companies_join" | |
# ON "memberships"."foundation_id" = "foundations_companies_join"."id" | |
# WHERE "businesses"."type" IN ('Foundation') <<< foundations_companies_join! <<<<<<<<<<<<<<<<< | |
# AND "businesses"."type" IN ('Company') | |
# AND "foundations_companies_join"."registry_id" = ? | |
def test_has_many_through_through_with_sti_aliasing | |
reg = Registry.first | |
regb = Registry.last | |
assert_equal 1, reg.companies.count | |
assert_equal 2, regb.companies.count | |
end | |
end |
uploaded newer version with ugly workaround
split stuff about #18062 => https://gist.github.com/nachokb/b12c26af8fa4740d1230
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
relevant for rails/rails#18061 and rails/rails#18062