Skip to content

Instantly share code, notes, and snippets.

@jemminger
Created October 24, 2017 03:59
Show Gist options
  • Save jemminger/b14dc10bc1501c9efce081729182de33 to your computer and use it in GitHub Desktop.
Save jemminger/b14dc10bc1501c9efce081729182de33 to your computer and use it in GitHub Desktop.
ActiveRecord bug with unscope() and a grouped RH side
# frozen_string_literal: true
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"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# Activate the gem you are reporting the issue against.
gem "activerecord", "5.1.3"
# Fixed here
# gem "activerecord", "5.1.3", github: "jemminger/rails", branch: "unscope-with-group"
gem "sqlite3"
end
require "active_record"
require "minitest/autorun"
require "logger"
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# 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 :posts, force: true do |t|
t.boolean :flagged
end
end
class Post < ActiveRecord::Base
default_scope -> { unflagged }
scope :flagged, -> { unscope(where: :flagged).where(flagged: true) }
scope :unflagged, -> { where(flagged: [nil, false]) }
end
class BugTest < Minitest::Test
def test_unscope
post1 = Post.create!
post2 = Post.create!(flagged: true)
assert_equal [post1], Post.all
assert_equal [post2], Post.flagged
end
end
# frozen_string_literal: true
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"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# Activate the gem you are reporting the issue against.
# gem "activerecord", "5.1.3"
# Fixed here
gem "activerecord", "5.1.3", github: "jemminger/rails", branch: "unscope-with-group"
gem "sqlite3"
end
require "active_record"
require "minitest/autorun"
require "logger"
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# 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 :posts, force: true do |t|
t.boolean :flagged
end
end
class Post < ActiveRecord::Base
default_scope -> { unflagged }
scope :flagged, -> { unscope(where: :flagged).where(flagged: true) }
scope :unflagged, -> { where(flagged: [nil, false]) }
end
class BugTest < Minitest::Test
def test_unscope
post1 = Post.create!
post2 = Post.create!(flagged: true)
assert_equal [post1], Post.all
assert_equal [post2], Post.flagged
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment