Skip to content

Instantly share code, notes, and snippets.

@agios
Created October 3, 2014 15:47
Show Gist options
  • Save agios/9fffc2cdaf7630f4eb68 to your computer and use it in GitHub Desktop.
Save agios/9fffc2cdaf7630f4eb68 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 'arel', github: 'rails/arel'
gem 'rack', github: 'rack/rack'
gem 'i18n', github: 'svenfuchs/i18n'
gem 'sqlite3'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require "#{`bundle show activerecord`.chomp}/test/cases/test_case"
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)
ActiveRecord::Schema.define do
create_table :posts do |t|
end
create_table :comments do |t|
t.integer :post_id
t.boolean :is_best
end
end
class Comment < ActiveRecord::Base
belongs_to :post
end
class Post < ActiveRecord::Base
has_many :comments
has_one :best_comment, -> { where(is_best: true) }, class_name: Comment
end
class BugTest < ActiveRecord::TestCase
def test_eager_load_uses_single_query
Post.create!.tap do |post|
post.comments << Comment.create!(is_best: true)
end
Post.create!
assert_queries 1 do
Post.eager_load(:best_comment).map{|post| post.best_comment}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment