From a878126028471626a9b30ebe60b7306374fb74ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 8 Feb 2012 06:41:54 -0800 Subject: Merge pull request #4942 from bogdan/pluck_joins AR::Relation#pluck: improve to work with joins Conflicts: activerecord/lib/active_record/relation/calculations.rb activerecord/test/cases/calculations_test.rb --- activerecord/CHANGELOG.md | 4 ++++ activerecord/lib/active_record/relation/calculations.rb | 7 ++++++- activerecord/test/cases/calculations_test.rb | 10 ++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 5ff0d1e204..68a48e6431 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,9 @@ ## Rails 3.2.10 (unreleased) +* Fix `pluck` to work with joins. Backport of #4942. + + *Carlos Antonio da Silva* + * Fix a problem with `translate_exception` method in a non English environment. Backport of #6397. diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index 45e9e64229..20c5fa03a2 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -177,7 +177,12 @@ module ActiveRecord # Person.where(:confirmed => true).limit(5).pluck(:id) # def pluck(column_name) - column_name = column_name.to_s + if column_name.is_a?(Symbol) && column_names.include?(column_name.to_s) + column_name = "#{table_name}.#{column_name}" + else + column_name = column_name.to_s + end + relation = clone relation.select_values = [column_name] klass.connection.select_all(relation.arel, nil, bind_values).map! do |attributes| diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index 63383bded9..ab573d8cc8 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -493,4 +493,14 @@ class CalculationsTest < ActiveRecord::TestCase assert_equal [1,2,3,4], taks_relation.pluck(:id) assert_equal [false, true, true, true], taks_relation.pluck(:approved) end + + def test_pluck_auto_table_name_prefix + c = Company.create!(:name => "test", :contracts => [Contract.new]) + assert_equal [c.id], Company.joins(:contracts).pluck(:id) + end + + def test_pluck_not_auto_table_name_prefix_if_column_joined + c = Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)]) + assert_equal [7], Company.joins(:contracts).pluck(:developer_id).map(&:to_i) + end end -- cgit v1.2.3