Skip to content

Instantly share code, notes, and snippets.

@jturkel
Created June 22, 2014 04:04
Show Gist options
  • Save jturkel/1e6355e8de2bf9953945 to your computer and use it in GitHub Desktop.
Save jturkel/1e6355e8de2bf9953945 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 'sqlite3'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'minitest/autorun'
require 'logger'
# 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(:blogs)
ActiveRecord::Base.connection.create_table(:posts) do |t|
t.integer :blog_id
end
end
# Models
class Blog < ActiveRecord::Base
has_many :posts
has_many :limited_posts, -> { order(:id).limit(2) }, class_name: 'Post'
has_many :grouped_posts, -> { group(:blog_id) }, class_name: 'Post'
has_many :offset_posts, -> { offset(2) }, class_name: 'Post'
end
class Post < ActiveRecord::Base
belongs_to :blog
end
class BugTest < Minitest::Test
def setup
Blog.delete_all
Post.delete_all
end
def test_eager_load_association_with_limit
blog = Blog.create!
3.times { blog.posts.create! }
blog = Blog.includes(:limited_posts).first!
assert_equal(2, blog.limited_posts.size)
assert_equal(Post.order(:id).limit(2), blog.limited_posts)
end
def test_eager_load_association_with_group
blog = Blog.create!
3.times { blog.posts.create! }
blog = Blog.includes(:grouped_posts).first!
assert_equal(1, blog.grouped_posts.size)
end
def test_eager_load_association_with_offset
blog = Blog.create!
3.times { blog.posts.create! }
blog = Blog.includes(:offset_posts).first!
assert_equal(1, blog.offset_posts.size)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment