Skip to content

Instantly share code, notes, and snippets.

@arivero
Forked from jturkel/rails_issue_11078.rb
Created April 25, 2019 19:02
Show Gist options
  • Save arivero/e1aa7acce2771bd083b08589e2bcd99d to your computer and use it in GitHub Desktop.
Save arivero/e1aa7acce2771bd083b08589e2bcd99d to your computer and use it in GitHub Desktop.
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'sqlite3'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'minitest/autorun'
require 'logger'
puts "Using ActiveRecord #{ActiveRecord::VERSION::STRING}"
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
# Schema
ActiveRecord::Schema.define do
ActiveRecord::Base.connection.create_table(:groups) do |t|
end
ActiveRecord::Base.connection.create_table(:users) do |t|
t.integer :group_id
end
ActiveRecord::Base.connection.create_table(:posts) do |t|
t.integer :user_id
t.string :type
end
end
# Models
class Group < ActiveRecord::Base
has_many :users
has_many :articles, through: :users
end
class User < ActiveRecord::Base
belongs_to :group
has_many :articles
end
class Post < ActiveRecord::Base
belongs_to :user
end
class Article < Post
end
class BugTest < Minitest::Test
def test_bug
group = Group.create!
user = group.users.create!
article = Article.create!(user: user)
post = Post.create!(user: user)
# Works without eager loading
groups = Group.all.to_a
assert_equal([article], groups.flat_map(&:articles))
# Fails with eager loading
groups = Group.includes(:articles).to_a
assert_equal([article], groups.flat_map(&:articles))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment