From f138bea5da463509c0dc51283dd35e803796a77a Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Thu, 14 Jan 2016 13:35:48 -0700 Subject: [PATCH] Properly include the `from` clause when merging relations The code that set the from clause was removed in bdc5141652770fd227455681cde1f9899f55b0b9. I did not give any reason for doing so. My assumption was that I intended to change it to use the clause objects, but forgot. We appeared to not have test coverage for this case. Fixes #22996 --- activerecord/lib/active_record/relation/merger.rb | 3 +++ activerecord/test/cases/relation/merging_test.rb | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/activerecord/lib/active_record/relation/merger.rb b/activerecord/lib/active_record/relation/merger.rb index cb971eb255..396638d74d 100644 --- a/activerecord/lib/active_record/relation/merger.rb +++ b/activerecord/lib/active_record/relation/merger.rb @@ -141,6 +141,9 @@ module ActiveRecord end def merge_single_values + if relation.from_clause.empty? + relation.from_clause = other.from_clause + end relation.lock_value ||= other.lock_value unless other.create_with_value.blank? diff --git a/activerecord/test/cases/relation/merging_test.rb b/activerecord/test/cases/relation/merging_test.rb index 0a2e874e4f..60a806c05a 100644 --- a/activerecord/test/cases/relation/merging_test.rb +++ b/activerecord/test/cases/relation/merging_test.rb @@ -104,6 +104,13 @@ class RelationMergingTest < ActiveRecord::TestCase post = PostThatLoadsCommentsInAnAfterSaveHook.create!(title: "First Post", body: "Blah blah blah.") assert_equal "First comment!", post.comments.where(body: "First comment!").first_or_create.body end + + def test_merging_with_from_clause + relation = Post.all + assert relation.from_clause.empty? + relation = relation.merge(Post.from("posts")) + refute relation.from_clause.empty? + end end class MergingDifferentRelationsTest < ActiveRecord::TestCase