From 19dc69cb07572f8b17ae15c8bc0b5a29bc8a281f Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Mon, 20 Mar 2017 14:55:53 -0700 Subject: Fix where_clause#except with specific where value Fixes a regression introduced in 22ca710f20c3c656811df006cbf1f4dbc359f7a6 where Relation#unscope with a specific where value (vs unscoping the entire where clause) could result in the wrong binds being left on the query. This was caused by an index variable not being incremented properly. --- activerecord/lib/active_record/relation/where_clause.rb | 4 ++-- activerecord/test/cases/relations_test.rb | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/activerecord/lib/active_record/relation/where_clause.rb b/activerecord/lib/active_record/relation/where_clause.rb index 417b24c7bb..119910ee79 100644 --- a/activerecord/lib/active_record/relation/where_clause.rb +++ b/activerecord/lib/active_record/relation/where_clause.rb @@ -148,10 +148,10 @@ module ActiveRecord (binds_index...(binds_index + binds_contains)).each do |i| except_binds[i] = true end - - binds_index += binds_contains end + binds_index += binds_contains if binds_contains + except end diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 73986d2d16..e17cb1795e 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -1970,6 +1970,14 @@ class RelationTest < ActiveRecord::TestCase assert_equal p2.first.comments, comments end + def test_unscope_specific_where_value + posts = Post.where(title: "Welcome to the weblog", body: "Such a lovely day") + + assert_equal 1, posts.count + assert_equal 1, posts.unscope(where: :title).count + assert_equal 1, posts.unscope(where: :body).count + end + def test_unscope_removes_binds left = Post.where(id: Arel::Nodes::BindParam.new) column = Post.columns_hash["id"] -- cgit v1.2.3