Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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