Skip to content

Instantly share code, notes, and snippets.

@iantropov
Created November 10, 2013 06:31
Show Gist options
  • Save iantropov/7394625 to your computer and use it in GitHub Desktop.
Save iantropov/7394625 to your computer and use it in GitHub Desktop.
Test case for Rails issue #5588
gemfile_path = "#{File.basename(__FILE__, '.rb')}.gemfile"
ENV['BUNDLE_GEMFILE'] = File.expand_path(gemfile_path)
unless File.exists?(gemfile_path)
File.write(gemfile_path, <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', '4.0.0'
gem 'sqlite3'
GEMFILE
system "bundle --gemfile=#{gemfile_path} --clean"
end
require 'bundler'
Bundler.setup(:default)
# Activate the gem you are reporting the issue against.
require 'active_record'
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 :titles do |t|
t.string :name
t.timestamps
end
create_table :comments do |t|
t.string :name
t.references :title
t.timestamps
end
end
class Comment < ActiveRecord::Base
belongs_to :title
end
class Title < ActiveRecord::Base
has_many :comments, dependent: :delete_all
end
class BugTest < MiniTest::Unit::TestCase
def test_order
title_1 = Title.create!(name: 'Awesome title')
Comment.create!(name: 'title1', title: title_1)
title_2 = Title.create!(name: 'Awesome title')
Comment.create!(name: 'title1', title: title_2)
Comment.create!(name: 'title2', title: title_2)
assert_equal [title_2, title_1], Title.select('titles.*, count(comments.id) AS comments_count').
joins(:comments).
group('titles.id').
order('comments_count DESC').
to_a
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment