aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorBogdan Gusiev <agresso@gmail.com>2014-08-14 13:44:29 +0300
committerBogdan Gusiev <agresso@gmail.com>2014-08-14 13:44:29 +0300
commitfe67dfbbeea092f0f42e81e4901fe9a949cf9484 (patch)
tree4749d661e781a38433c2424f3177d0c200f23c35 /activerecord
parent3300fdedc748993b378288c6cbc3113885c955ed (diff)
downloadrails-fe67dfbbeea092f0f42e81e4901fe9a949cf9484.tar.gz
rails-fe67dfbbeea092f0f42e81e4901fe9a949cf9484.tar.bz2
rails-fe67dfbbeea092f0f42e81e4901fe9a949cf9484.zip
Fixed AR::Relation#where edge case with Hash and other Relation
Example: Author.where(posts: { author_id: Author.where(country_id: 1) }).joins(:posts)
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb18
-rw-r--r--activerecord/test/cases/relation/where_test.rb9
2 files changed, 24 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index 1262b2c291..c8f382ad78 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -952,9 +952,7 @@ WARNING
self.bind_values += bind_values
attributes = @klass.send(:expand_hash_conditions_for_aggregates, tmp_opts)
- attributes.values.grep(ActiveRecord::Relation) do |rel|
- self.bind_values += rel.bind_values
- end
+ add_relations_to_bind_values(attributes)
PredicateBuilder.build_from_hash(klass, attributes, table)
else
@@ -1137,5 +1135,19 @@ WARNING
raise ArgumentError, "The method .#{method_name}() must contain arguments."
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
diff --git a/activerecord/test/cases/relation/where_test.rb b/activerecord/test/cases/relation/where_test.rb
index a6a36a6fd9..b4804aa9d7 100644
--- a/activerecord/test/cases/relation/where_test.rb
+++ b/activerecord/test/cases/relation/where_test.rb
@@ -61,6 +61,15 @@ module ActiveRecord
assert_equal expected.to_sql, actual.to_sql
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
treasure = Treasure.new
treasure.id = 1