aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-06-11 18:13:50 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-06-11 18:13:50 -0300
commit1ed2c3cf6a76b0b18d5db36b453324aee175c8c8 (patch)
treed173e9b6834bd0bc2c795e6ce5f5131f82b4f6bf /activerecord
parent1b14bff81a9ab6654badf52b552f90c8fc3ab0e3 (diff)
parentfef044cd2bd167201462861385ebf38bbf3158d7 (diff)
downloadrails-1ed2c3cf6a76b0b18d5db36b453324aee175c8c8.tar.gz
rails-1ed2c3cf6a76b0b18d5db36b453324aee175c8c8.tar.bz2
rails-1ed2c3cf6a76b0b18d5db36b453324aee175c8c8.zip
Merge pull request #15652 from sgrif/pluck-multiple-named-columns
Pluck should work with columns of the same name from different tables
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md7
-rw-r--r--activerecord/lib/active_record/relation/calculations.rb4
-rw-r--r--activerecord/test/cases/calculations_test.rb7
3 files changed, 15 insertions, 3 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 8e958a851e..e0a7552921 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,10 @@
+* Pluck now works when selecting columns from different tables with the same
+ name.
+
+ Fixes #15649
+
+ *Sean Griffin*
+
* Remove `cache_attributes` and friends. All attributes are cached.
*Sean Griffin*
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb
index 38970a66ae..028e4d80ab 100644
--- a/activerecord/lib/active_record/relation/calculations.rb
+++ b/activerecord/lib/active_record/relation/calculations.rb
@@ -176,9 +176,7 @@ module ActiveRecord
}
end
- result = result.map do |attributes|
- values = attributes.values
-
+ result = result.rows.map do |values|
columns.zip(values).map { |column, value| column.type_cast_from_database value }
end
columns.one? ? result.map!(&:first) : result
diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb
index dfc6a7ec67..222b1505a8 100644
--- a/activerecord/test/cases/calculations_test.rb
+++ b/activerecord/test/cases/calculations_test.rb
@@ -610,4 +610,11 @@ class CalculationsTest < ActiveRecord::TestCase
assert_equal [1,2,3,4,5], taks_relation.pluck(:id)
assert_equal [false, true, true, true, true], taks_relation.pluck(:approved)
end
+
+ def test_pluck_columns_with_same_name
+ expected = [["The First Topic", "The Second Topic of the day"], ["The Third Topic of the day", "The Fourth Topic of the day"]]
+ actual = Topic.joins(:replies)
+ .pluck('topics.title', 'replies_topics.title')
+ assert_equal expected, actual
+ end
end