Skip to content

Instantly share code, notes, and snippets.

@NobodysNightmare
Last active February 3, 2016 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NobodysNightmare/45992e1020ec092b9e4b to your computer and use it in GitHub Desktop.
Save NobodysNightmare/45992e1020ec092b9e4b to your computer and use it in GitHub Desktop.
Reproduction Testcase for missing expansion of :references inside a subquery
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' # always using the latest stable AR
# gem 'activerecord', '5.0.0.beta2' # or use the latest beta build of AR 5
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 :blubbs, force: true do |t|
t.integer "foo_id", limit: 4
end
create_table :foos, force: true do |t|
t.integer "bar_id", limit: 4
end
create_table :bars, force: true do |t|
t.integer :value
end
end
class Blubb < ActiveRecord::Base
belongs_to :foo
end
class Foo < ActiveRecord::Base
belongs_to :bar
end
class Bar < ActiveRecord::Base
end
class BugTest < Minitest::Test
def test_reference_expansion_in_subquery
Blubb.create!(foo: Foo.create!(bar: Bar.create!(value: 1)))
Blubb.create!(foo: Foo.create!(bar: Bar.create!(value: 5)))
foos = Foo.includes(:bar).references(:bar).where('bars.value > 3')
assert_equal 1, foos.count
# workaround: where(foo: foos.to_a) will work correctly, since this avoids embedding
# "foos" as subquery
blubbs = Blubb.where(foo: foos)
assert_equal 1, blubbs.count
assert_equal 5, blubbs.first.foo.bar.value
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment