Skip to content

Commit ed68048

Browse files
committed
Fix unscope not working when where by tripe dot range
Fix rails#48094
1 parent 84540a6 commit ed68048

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

activerecord/CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
* Fix unscope is not working in specific case
2+
3+
Before:
4+
```ruby
5+
Post.where(id: 1...3).unscope(where: :id).to_sql # "SELECT `posts`.* FROM `posts` WHERE `posts`.`id` >= 1 AND `posts`.`id` < 3"
6+
7+
```
8+
9+
After:
10+
```ruby
11+
Post.where(id: 1...3).unscope(where: :id).to_sql # "SELECT `posts`.* FROM `posts`"
12+
```
13+
14+
Fixes: #48094
15+
16+
*Kazuya Hatanaka*
17+
118
* Allow composite primary key to be derived from schema
219

320
Booting an application with a schema that contains composite primary keys

activerecord/lib/arel/nodes/and.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ def right
1818
children[1]
1919
end
2020

21+
def fetch_attribute(&block)
22+
children.any? && children.all? { |child| child.fetch_attribute(&block) }
23+
end
24+
2125
def hash
2226
children.hash
2327
end

activerecord/test/cases/relations_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2329,6 +2329,20 @@ def test_unscope_grouped_where
23292329
assert_equal Post.count, posts.unscope(where: :title).count
23302330
end
23312331

2332+
def test_unscope_with_double_dot_where
2333+
posts = Post.where(id: 1..2)
2334+
2335+
assert_equal 2, posts.count
2336+
assert_equal Post.count, posts.unscope(where: :id).count
2337+
end
2338+
2339+
def test_unscope_with_triple_dot_where
2340+
posts = Post.where(id: 1...3)
2341+
2342+
assert_equal 2, posts.count
2343+
assert_equal Post.count, posts.unscope(where: :id).count
2344+
end
2345+
23322346
def test_locked_should_not_build_arel
23332347
posts = Post.locked
23342348
assert_predicate posts, :locked?

0 commit comments

Comments
 (0)