diff options
author | Sean Griffin <sean@seantheprogrammer.com> | 2016-01-14 13:35:48 -0700 |
---|---|---|
committer | Sean Griffin <sean@seantheprogrammer.com> | 2016-01-14 13:36:30 -0700 |
commit | f138bea5da463509c0dc51283dd35e803796a77a (patch) | |
tree | 81392a41912fd35f5aebe9290fc504f4afd68ee8 | |
parent | b7cce1c0a92ef88341137290eefd19ef688c175b (diff) | |
download | rails-f138bea5da463509c0dc51283dd35e803796a77a.tar.gz rails-f138bea5da463509c0dc51283dd35e803796a77a.tar.bz2 rails-f138bea5da463509c0dc51283dd35e803796a77a.zip |
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
-rw-r--r-- | activerecord/lib/active_record/relation/merger.rb | 3 | ||||
-rw-r--r-- | activerecord/test/cases/relation/merging_test.rb | 7 |
2 files changed, 10 insertions, 0 deletions
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 |