提交 71e8f199 编写于 作者: S Santiago Pastorino

Merge pull request #16502 from bogdan/where-hash-nested-relation

[Regression 4.0 -> 4.1] Fixed AR::Relation#where edge case with Hash and other Relation
...@@ -952,9 +952,7 @@ def build_where(opts, other = []) ...@@ -952,9 +952,7 @@ def build_where(opts, other = [])
self.bind_values += bind_values self.bind_values += bind_values
attributes = @klass.send(:expand_hash_conditions_for_aggregates, tmp_opts) attributes = @klass.send(:expand_hash_conditions_for_aggregates, tmp_opts)
attributes.values.grep(ActiveRecord::Relation) do |rel| add_relations_to_bind_values(attributes)
self.bind_values += rel.bind_values
end
PredicateBuilder.build_from_hash(klass, attributes, table) PredicateBuilder.build_from_hash(klass, attributes, table)
else else
...@@ -1137,5 +1135,19 @@ def check_if_method_has_arguments!(method_name, args) ...@@ -1137,5 +1135,19 @@ def check_if_method_has_arguments!(method_name, args)
raise ArgumentError, "The method .#{method_name}() must contain arguments." raise ArgumentError, "The method .#{method_name}() must contain arguments."
end end
end end
# This function is recursive just for better readablity.
# #where argument doesn't support more than one level nested hash in real world.
def add_relations_to_bind_values(attributes)
if attributes.is_a?(Hash)
attributes.each_value do |value|
if value.is_a?(ActiveRecord::Relation)
self.bind_values += value.bind_values
else
add_relations_to_bind_values(value)
end
end
end
end
end end
end end
...@@ -61,6 +61,15 @@ def test_belongs_to_nested_where ...@@ -61,6 +61,15 @@ def test_belongs_to_nested_where
assert_equal expected.to_sql, actual.to_sql assert_equal expected.to_sql, actual.to_sql
end end
def test_belongs_to_nested_where_with_relation
author = authors(:david)
expected = Author.where(id: author ).joins(:posts)
actual = Author.where(posts: { author_id: Author.where(id: author.id) }).joins(:posts)
assert_equal expected.to_a, actual.to_a
end
def test_polymorphic_shallow_where def test_polymorphic_shallow_where
treasure = Treasure.new treasure = Treasure.new
treasure.id = 1 treasure.id = 1
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册