-
-
Save arivero/e1aa7acce2771bd083b08589e2bcd99d to your computer and use it in GitHub Desktop.
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
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