Skip to content

Commit f6666de

Browse files
authored
Merge pull request rails#51941 from Liamjen/allow_unscope_cte
Allow unscoping ActiveRecord Query CTEs
2 parents c82c49c + 07b1f22 commit f6666de

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

activerecord/lib/active_record/relation/query_methods.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ def reorder!(*args) # :nodoc:
751751
VALID_UNSCOPING_VALUES = Set.new([:where, :select, :group, :order, :lock,
752752
:limit, :offset, :joins, :left_outer_joins, :annotate,
753753
:includes, :eager_load, :preload, :from, :readonly,
754-
:having, :optimizer_hints])
754+
:having, :optimizer_hints, :with])
755755

756756
# Removes an unwanted relation that is already defined on a chain of relations.
757757
# This is useful when passing around chains of relations and would like to

activerecord/test/cases/relation/with_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ def test_raises_when_using_block
114114
Post.with(attributes_for_inspect: :id) { }
115115
end
116116
end
117+
118+
def test_unscoping
119+
relation = Post.with(posts_with_comments: Post.where("legacy_comments_count > 0"))
120+
121+
assert_equal true, relation.values[:with].flat_map(&:keys).include?(:posts_with_comments)
122+
relation = relation.unscope(:with)
123+
assert_nil relation.values[:with]
124+
assert_equal Post.count, relation.count
125+
end
117126
else
118127
def test_common_table_expressions_are_unsupported
119128
assert_raises ActiveRecord::StatementInvalid do

0 commit comments

Comments
 (0)