1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix unscope with subquery

Currently cannot unscope subquery properly.
This commit fixes the issue.

Fixes #26323.
This commit is contained in:
Ryuta Kamizono 2016-11-21 05:01:41 +09:00
parent 66076f381a
commit 22ca710f20
2 changed files with 31 additions and 10 deletions

View file

@ -25,10 +25,7 @@ module ActiveRecord
end
def except(*columns)
WhereClause.new(
predicates_except(columns),
binds_except(columns),
)
WhereClause.new(*except_predicates_and_binds(columns))
end
def or(other)
@ -132,20 +129,32 @@ module ActiveRecord
end
end
def predicates_except(columns)
predicates.reject do |node|
def except_predicates_and_binds(columns)
except_binds = []
binds_index = 0
predicates = self.predicates.reject do |node|
case node
when Arel::Nodes::Between, Arel::Nodes::In, Arel::Nodes::NotIn, Arel::Nodes::Equality, Arel::Nodes::NotEqual, Arel::Nodes::LessThan, Arel::Nodes::LessThanOrEqual, Arel::Nodes::GreaterThan, Arel::Nodes::GreaterThanOrEqual
binds_contains = node.grep(Arel::Nodes::BindParam).size
subrelation = (node.left.kind_of?(Arel::Attributes::Attribute) ? node.left : node.right)
columns.include?(subrelation.name.to_s)
end.tap do |except|
if except && binds_contains > 0
(binds_index...(binds_index + binds_contains)).each do |i|
except_binds[i] = true
end
binds_index += binds_contains
end
end
end
end
def binds_except(columns)
binds.reject do |attr|
columns.include?(attr.name)
binds = self.binds.reject.with_index do |_, i|
except_binds[i]
end
[predicates, binds]
end
def predicates_with_wrapped_sql_literals

View file

@ -1962,6 +1962,18 @@ class RelationTest < ActiveRecord::TestCase
assert !Post.all.respond_to?(:by_lifo)
end
def test_unscope_with_subquery
p1 = Post.where(id: 1)
p2 = Post.where(id: 2)
assert_not_equal p1, p2
comments = Comment.where(post: p1).unscope(where: :post_id).where(post: p2)
assert_not_equal p1.first.comments, comments
assert_equal p2.first.comments, comments
end
def test_unscope_removes_binds
left = Post.where(id: Arel::Nodes::BindParam.new)
column = Post.columns_hash["id"]