aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2013-05-21 10:55:56 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2013-05-21 10:55:56 -0700
commit50823452f70341447a010df27dd514fd97bfe8a9 (patch)
tree35f71e388e63a314bb7c356ca928c056a5373506 /activerecord
parentd2d5f15f0729d00132a240dd9e5169dce5962a0d (diff)
downloadrails-50823452f70341447a010df27dd514fd97bfe8a9.tar.gz
rails-50823452f70341447a010df27dd514fd97bfe8a9.tar.bz2
rails-50823452f70341447a010df27dd514fd97bfe8a9.zip
remove bind values for where clauses that were removed
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/relation/merger.rb17
-rw-r--r--activerecord/test/cases/relations_test.rb10
2 files changed, 18 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/relation/merger.rb b/activerecord/lib/active_record/relation/merger.rb
index 38f49912d1..a65ef1898a 100644
--- a/activerecord/lib/active_record/relation/merger.rb
+++ b/activerecord/lib/active_record/relation/merger.rb
@@ -99,13 +99,15 @@ module ActiveRecord
end
def merge_multi_values
- rhs_wheres = values[:where] || []
lhs_wheres = relation.where_values
+ rhs_wheres = values[:where] || []
+ lhs_binds = relation.bind_values
+ rhs_binds = values[:bind] || []
- _, kept = partition_overwrites(lhs_wheres, rhs_wheres)
+ removed, kept = partition_overwrites(lhs_wheres, rhs_wheres)
relation.where_values = kept + rhs_wheres
- relation.bind_values = merged_binds
+ relation.bind_values = filter_binds(lhs_binds, removed) + rhs_binds
if values[:reordering]
# override any order specified in the original relation
@@ -128,12 +130,9 @@ module ActiveRecord
end
end
- def merged_binds
- if values[:bind]
- (relation.bind_values + values[:bind]).uniq(&:first)
- else
- relation.bind_values
- end
+ def filter_binds(lhs_binds, removed_wheres)
+ set = Set.new removed_wheres.map { |x| x.left.name }
+ lhs_binds.dup.delete_if { |col,_| set.include? col.name }
end
# Remove equalities from the existing relation with a LHS which is
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index cf6af4e8f4..3c1fae78ce 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -1546,4 +1546,14 @@ class RelationTest < ActiveRecord::TestCase
assert merged.to_sql.include?("wtf")
assert merged.to_sql.include?("bbq")
end
+
+ def test_merging_removes_rhs_bind_parameters
+ left = Post.where(id: Arel::Nodes::BindParam.new('?'))
+ column = Post.columns_hash['id']
+ left.bind_values += [[column, 20]]
+ right = Post.where(id: 10)
+
+ merged = left.merge(right)
+ assert_equal [], merged.bind_values
+ end
end