Skip to content

Instantly share code, notes, and snippets.

@jturkel
Created July 10, 2018 17:49
Show Gist options
  • Save jturkel/c46e0297478c9acc2bc177d496009877 to your computer and use it in GitHub Desktop.
Save jturkel/c46e0297478c9acc2bc177d496009877 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.2.0' # Fails with '~> 5.1.0'
gem 'sqlite3', '1.3.12'
gem 'goldiloader'
end
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!
article1 = user.articles.create!
article2 = user.articles.create!
post = Post.create!(user: user)
# Explicit eager loading
articles = Group.includes(:articles).flat_map(&:articles).sort_by(&:id)
assert_equal([article1, article2], articles)
# Automatic eager loading
articles = Group.all.flat_map(&:articles).sort_by(&:id)
assert_equal([article1, article2], articles)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment