Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #12759 from dmarkow/backport-9675c7d

Backport relation merger updates from 9675c7d to 4-0-stable
  • Loading branch information...
commit 5827374d46a61cd1208eb307fbdf3e82e90cf957 2 parents c263a9a + aabf209
@rafaelfranca rafaelfranca authored
View
20 activerecord/lib/active_record/relation/merger.rb
@@ -107,13 +107,29 @@ def merge_joins
def merge_multi_values
lhs_wheres = relation.where_values
rhs_wheres = values[:where] || []
+
lhs_binds = relation.bind_values
rhs_binds = values[:bind] || []
removed, kept = partition_overwrites(lhs_wheres, rhs_wheres)
- relation.where_values = kept + rhs_wheres
- relation.bind_values = filter_binds(lhs_binds, removed) + rhs_binds
+ where_values = kept + rhs_wheres
+ bind_values = filter_binds(lhs_binds, removed) + rhs_binds
+
+ conn = relation.klass.connection
+ bviter = bind_values.each.with_index
+ where_values.map! do |node|
+ if Arel::Nodes::Equality === node && Arel::Nodes::BindParam === node.right
+ (column, _), i = bviter.next
+ substitute = conn.substitute_at column, i
+ Arel::Nodes::Equality.new(node.left, substitute)
+ else
+ node
+ end
+ end
+
+ relation.where_values = where_values
+ relation.bind_values = bind_values
if values[:reordering]
# override any order specified in the original relation
View
9 activerecord/test/cases/relation_test.rb
@@ -9,6 +9,9 @@ class RelationTest < ActiveRecord::TestCase
fixtures :posts, :comments, :authors
class FakeKlass < Struct.new(:table_name, :name)
+ def self.connection
+ Post.connection
+ end
end
def test_construction
@@ -214,6 +217,10 @@ class FakeKlass < Struct.new(:table_name, :name)
def arel_table
Post.arel_table
end
+
+ def connection
+ Post.connection
+ end
end
def relation
@@ -298,7 +305,7 @@ def relation
assert_equal({foo: 'bar'}, relation.create_with_value)
end
- test 'merge!' do
+ def test_merge!
assert relation.merge!(where: :foo).equal?(relation)
assert_equal [:foo], relation.where_values
end
View
17 activerecord/test/cases/relations_test.rb
@@ -1609,4 +1609,21 @@ def test_merging_removes_lhs_bind_parameters
merged = left.merge(right)
assert_equal [], merged.bind_values
end
+
+ def test_merging_reorders_bind_params
+ post = Post.first
+ id_column = Post.columns_hash['id']
+ title_column = Post.columns_hash['title']
+
+ bv = Post.connection.substitute_at id_column, 0
+
+ right = Post.where(id: bv)
+ right.bind_values += [[id_column, post.id]]
+
+ left = Post.where(title: bv)
+ left.bind_values += [[title_column, post.title]]
+
+ merged = left.merge(right)
+ assert_equal post, merged.first
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.