Permalink
Browse files

reduce the number of queries on IN clauses, fix relation queries in `…

…where`
  • Loading branch information...
1 parent de6a35b commit 170fb5c80c990688f5f372a3ba0e6cb75fb6edf0 @tenderlove tenderlove committed Jan 24, 2013
@@ -66,7 +66,7 @@ def self.references(attributes)
private
def self.build(attribute, value)
case value
- when Array, ActiveRecord::Associations::CollectionProxy
+ when Array
values = value.to_a.map {|x| x.is_a?(Base) ? x.id : x}
ranges, values = values.partition {|v| v.is_a?(Range)}
@@ -764,6 +764,11 @@ def build_where(opts, other = [])
[@klass.send(:sanitize_sql, other.empty? ? opts : ([opts] + other))]
when Hash
attributes = @klass.send(:expand_hash_conditions_for_aggregates, opts)
+
+ attributes.values.grep(ActiveRecord::Relation) do |rel|
+ self.bind_values += rel.bind_values
+ end
+
PredicateBuilder.build_from_hash(klass, attributes, table)
else
[opts]
@@ -8,7 +8,20 @@
module ActiveRecord
class WhereTest < ActiveRecord::TestCase
- fixtures :posts, :edges
+ fixtures :posts, :edges, :authors
+
+ def test_where_copies_bind_params
+ author = authors(:david)
+ posts = author.posts.where('posts.id != 1')
+ joined = Post.where(id: posts)
+
+ assert_operator joined.length, :>, 0
+
+ joined.each { |post|
+ assert_equal author, post.author
+ assert_not_equal 1, post.id
+ }
+ end
def test_belongs_to_shallow_where
author = Author.new

1 comment on commit 170fb5c

💛 💚 ❤️ 💙 💜

Please sign in to comment.