Skip to content

Commit

Permalink
Fix unscope not working when where by tripe dot range
Browse files Browse the repository at this point in the history
Fix #48094
  • Loading branch information
ippachi committed Jun 13, 2023
1 parent 84540a6 commit ed68048
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
17 changes: 17 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
* Fix unscope is not working in specific case

Before:
```ruby
Post.where(id: 1...3).unscope(where: :id).to_sql # "SELECT `posts`.* FROM `posts` WHERE `posts`.`id` >= 1 AND `posts`.`id` < 3"

```

After:
```ruby
Post.where(id: 1...3).unscope(where: :id).to_sql # "SELECT `posts`.* FROM `posts`"
```

Fixes: #48094

*Kazuya Hatanaka*

* Allow composite primary key to be derived from schema

Booting an application with a schema that contains composite primary keys
Expand Down
4 changes: 4 additions & 0 deletions activerecord/lib/arel/nodes/and.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def right
children[1]
end

def fetch_attribute(&block)
children.any? && children.all? { |child| child.fetch_attribute(&block) }
end

def hash
children.hash
end
Expand Down
14 changes: 14 additions & 0 deletions activerecord/test/cases/relations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2329,6 +2329,20 @@ def test_unscope_grouped_where
assert_equal Post.count, posts.unscope(where: :title).count
end

def test_unscope_with_double_dot_where
posts = Post.where(id: 1..2)

assert_equal 2, posts.count
assert_equal Post.count, posts.unscope(where: :id).count
end

def test_unscope_with_triple_dot_where
posts = Post.where(id: 1...3)

assert_equal 2, posts.count
assert_equal Post.count, posts.unscope(where: :id).count
end

def test_locked_should_not_build_arel
posts = Post.locked
assert_predicate posts, :locked?
Expand Down

0 comments on commit ed68048

Please sign in to comment.