Skip to content

Commit 0924095

Browse files
committed
allow rewhere to pass nil and return unscope(:where)
1 parent 912096d commit 0924095

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

activerecord/lib/active_record/relation/query_methods.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,8 @@ def where!(opts, *rest) # :nodoc:
944944
# This is short-hand for <tt>unscope(where: conditions.keys).where(conditions)</tt>.
945945
# Note that unlike reorder, we're only unscoping the named conditions -- not the entire where statement.
946946
def rewhere(conditions)
947+
return unscope(:where) if conditions.nil?
948+
947949
scope = spawn
948950
where_clause = scope.build_where_clause(conditions)
949951

activerecord/test/cases/relation/where_chain_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,5 +183,12 @@ def test_rewhere_with_infinite_range
183183

184184
assert_equal expected.to_a, relation.to_a
185185
end
186+
187+
def test_rewhere_with_nil
188+
relation = Post.where(comments_count: 16).rewhere(nil)
189+
expected = Post.all
190+
191+
assert_equal expected.to_a, relation.to_a
192+
end
186193
end
187194
end

0 commit comments

Comments
 (0)