diff options
author | José Valim <jose.valim@plataformatec.com.br> | 2012-02-08 06:41:54 -0800 |
---|---|---|
committer | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-12-03 23:17:16 -0200 |
commit | a878126028471626a9b30ebe60b7306374fb74ca (patch) | |
tree | d801fbe07b897cc8ce83a3dab8ef7b0031950c6f /activerecord | |
parent | 1a5caf180e22970a5ca927e9315a1af9f6bf6495 (diff) | |
download | rails-a878126028471626a9b30ebe60b7306374fb74ca.tar.gz rails-a878126028471626a9b30ebe60b7306374fb74ca.tar.bz2 rails-a878126028471626a9b30ebe60b7306374fb74ca.zip |
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
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/relation/calculations.rb | 7 | ||||
-rw-r--r-- | activerecord/test/cases/calculations_test.rb | 10 |
3 files changed, 20 insertions, 1 deletions
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 |