diff options
| -rw-r--r-- | activerecord/lib/active_record/relation/predicate_builder.rb | 2 | ||||
| -rw-r--r-- | activerecord/lib/active_record/relation/query_methods.rb | 5 | ||||
| -rw-r--r-- | activerecord/test/cases/relation/where_test.rb | 15 | 
3 files changed, 20 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/relation/predicate_builder.rb b/activerecord/lib/active_record/relation/predicate_builder.rb index 330ec08535..537ebbef28 100644 --- a/activerecord/lib/active_record/relation/predicate_builder.rb +++ b/activerecord/lib/active_record/relation/predicate_builder.rb @@ -66,7 +66,7 @@ module ActiveRecord      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)} diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index 46c0d6206f..42849d6bc9 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -764,6 +764,11 @@ module ActiveRecord          [@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] diff --git a/activerecord/test/cases/relation/where_test.rb b/activerecord/test/cases/relation/where_test.rb index f69a248491..c43c7601a2 100644 --- a/activerecord/test/cases/relation/where_test.rb +++ b/activerecord/test/cases/relation/where_test.rb @@ -8,7 +8,20 @@ require 'models/edge'  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  | 
