aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-12-22 19:22:59 +0000
committerJon Leighton <j@jonathanleighton.com>2011-12-22 19:24:16 +0000
commite46e4915ffe153bc952b1fd5735c4b567e226ccc (patch)
tree1c12e681db627c4e2108eaeb0acb6b676dd4db07
parent7edade337e968fb028b2b6abfa579120eb424039 (diff)
downloadrails-e46e4915ffe153bc952b1fd5735c4b567e226ccc.tar.gz
rails-e46e4915ffe153bc952b1fd5735c4b567e226ccc.tar.bz2
rails-e46e4915ffe153bc952b1fd5735c4b567e226ccc.zip
Make ActiveRecord::Relation#pluck work with serialized attributes
-rw-r--r--activerecord/lib/active_record/relation/calculations.rb8
-rw-r--r--activerecord/test/cases/calculations_test.rb9
2 files changed, 12 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb
index 0f57e9831d..0d60810cd2 100644
--- a/activerecord/lib/active_record/relation/calculations.rb
+++ b/activerecord/lib/active_record/relation/calculations.rb
@@ -168,7 +168,7 @@ module ActiveRecord
# This method is designed to perform select by a single column as direct SQL query
# Returns <tt>Array</tt> with values of the specified column name
- # The values has same data type as column.
+ # The values has same data type as column.
#
# Examples:
#
@@ -177,9 +177,9 @@ module ActiveRecord
# Person.where(:confirmed => true).limit(5).pluck(:id)
#
def pluck(column_name)
- scope = self.select(column_name)
- self.connection.select_values(scope.to_sql).map! do |value|
- type_cast_using_column(value, column_for(column_name))
+ column_name = column_name.to_s
+ klass.connection.select_all(select(column_name).arel).map! do |attributes|
+ klass.type_cast_attribute(attributes.keys.first, klass.initialize_attributes(attributes))
end
end
diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb
index 5abf3d1af4..66c801ca75 100644
--- a/activerecord/test/cases/calculations_test.rb
+++ b/activerecord/test/cases/calculations_test.rb
@@ -458,7 +458,6 @@ class CalculationsTest < ActiveRecord::TestCase
assert_equal [ topic.approved ], relation.pluck(:approved)
assert_equal [ topic.last_read ], relation.pluck(:last_read)
assert_equal [ topic.written_on ], relation.pluck(:written_on)
-
end
def test_pluck_and_uniq
@@ -471,4 +470,12 @@ class CalculationsTest < ActiveRecord::TestCase
assert_equal [contract.id], company.contracts.pluck(:id)
end
+ def test_pluck_with_serialization
+ t = Topic.create!(:content => { :foo => :bar })
+ assert_equal [{:foo => :bar}], Topic.where(:id => t.id).pluck(:content)
+ end
+
+ def test_pluck_with_qualified_column_name
+ assert_equal [1,2,3,4], Topic.order(:id).pluck("topics.id")
+ end
end