Skip to content

Instantly share code, notes, and snippets.

@Liooo
Created December 4, 2018 01:33
Show Gist options
  • Save Liooo/ea4629204f7f3233633125e5f134b574 to your computer and use it in GitHub Desktop.
Save Liooo/ea4629204f7f3233633125e5f134b574 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', '5.1.2'
gem 'sqlite3'
gem 'ransack', require: false
end
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)
require 'ransack'
ActiveRecord::Schema.define do
create_table :a_ancestors, force: true do |t|
end
create_table :b_ancestors, force: true do |t|
end
create_table :c_ancestors, force: true do |t|
end
create_table :parents, force: true do |t|
t.integer :a_ancestor_id
t.integer :b_ancestor_id
t.integer :c_ancestor_id
end
create_table :children, force: true do |t|
t.integer :parent_id
end
end
class Children < ActiveRecord::Base
belongs_to :parent
has_one :a_ancestor, through: :parent
has_one :b_ancestor, through: :parent
has_one :c_ancestor, through: :parent
end
class Parent < ActiveRecord::Base
belongs_to :a_ancestor
belongs_to :b_ancestor
belongs_to :c_ancestor
end
class AAncestor < ActiveRecord::Base; end
class BAncestor < ActiveRecord::Base; end
class CAncestor < ActiveRecord::Base; end
class BugTest < Minitest::Test
def setup
c = Children.create
p = c.create_parent
p.create_a_ancestor
p.create_b_ancestor
p.create_c_ancestor
end
def test_duplicate_joins
puts sql = Children.ransack(
a_ancestor_id_eq: 1,
b_ancestor_id_eq: 1,
c_ancestor_id_eq: 1,
).result.to_sql
assert_equal 1, sql.scan('LEFT OUTER JOIN "parents"').count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment