Skip to content

Instantly share code, notes, and snippets.

@pekeler
Created January 15, 2014 04:39
Show Gist options
  • Save pekeler/8430850 to your computer and use it in GitHub Desktop.
Save pekeler/8430850 to your computer and use it in GitHub Desktop.
test case showing unscope(:order) not working
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'
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.integer :user_id
end
create_table :users do |t|
end
end
class Post < ActiveRecord::Base
has_many :comments, -> {order(:id)}
has_many :users, -> {unscope(:order).uniq}, :through => :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
belongs_to :user
end
class User < ActiveRecord::Base
end
class BugTest < Minitest::Test
def test_uniq_through_removes_order_to_work_with_postgresql
post = Post.create!
puts Gem.loaded_specs["activerecord"].version.to_s
puts post.users.to_sql
assert(!(post.users.to_sql =~ /order by/i), "generated sql includes ORDER BY")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment