Skip to content

Instantly share code, notes, and snippets.

@phoet
Created February 4, 2019 11:18
Show Gist options
  • Save phoet/6d42e95028869ef55cfc6bd3a0973489 to your computer and use it in GitHub Desktop.
Save phoet/6d42e95028869ef55cfc6bd3a0973489 to your computer and use it in GitHub Desktop.
distinct and include
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", '6.0.0.beta1'
# gem "rails", '5.2.1.1'
gem 'pg'
end
require 'pg'
require "active_record"
require 'active_support'
require "minitest/autorun"
require "logger"
ActiveRecord::Base.establish_connection(adapter: "postgresql", database: "penseo_test2")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
t.timestamps(null: false)
end
create_table :comments, force: true do |t|
t.integer :post_id
t.timestamps(null: false)
end
end
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
class BugTest < Minitest::Test
def test_fails_with_distinct_size
post = Post.create!
post.comments << Comment.create!
assert Comment.where('posts.created_at > ?', 4.days.ago).includes(:post).distinct.order('posts.created_at').to_a
assert Comment.where('posts.created_at > ?', 4.days.ago).references(:post).distinct.order('posts.created_at').to_a
assert Comment.where('posts.created_at > ?', 4.days.ago).joins(:post).includes(:post).distinct.order('posts.created_at').to_a
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment