Skip to content

Instantly share code, notes, and snippets.

@swrobel
Last active February 2, 2016 00:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swrobel/7247955f409f4b24f253 to your computer and use it in GitHub Desktop.
Save swrobel/7247955f409f4b24f253 to your computer and use it in GitHub Desktop.
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', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
end
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)
class Vehicle < ActiveRecord::Base
self.abstract_class = true
scope :has_tires, -> { where.not(tires_count: nil) }
scope :by_weight, -> { order(weight: :desc) }
default_scope -> { has_tires.by_weight }
end
class Bus < Vehicle;end
class BugTest < Minitest::Test
def test_default_scope_with_where
where_pattern = /"buses"."tires_count"/
assert_match where_pattern, Bus.unscoped.has_tires.to_sql
assert_match where_pattern, Bus.all.to_sql # Fails
end
def test_default_scope_with_order
order_pattern = /"buses"."weight"/
assert_match order_pattern, Bus.unscoped.by_weight.to_sql
assert_match order_pattern, Bus.all.to_sql # Fails
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment