diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-11-30 01:41:09 -0800 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-11-30 01:41:09 -0800 |
commit | 271308cb9617b86cd4de20b629be504b48aea537 (patch) | |
tree | d713dd695abdcb09f1c191f1844166a27bf8ba38 /activerecord/lib/active_record/relation/calculations.rb | |
parent | 38ab982cfff98570b5f12933cff489364845789c (diff) | |
parent | a382d60f6abc94b6a965525872f858e48abc00de (diff) | |
download | rails-271308cb9617b86cd4de20b629be504b48aea537.tar.gz rails-271308cb9617b86cd4de20b629be504b48aea537.tar.bz2 rails-271308cb9617b86cd4de20b629be504b48aea537.zip |
Merge pull request #1915 from bogdan/active_record_map
ActiveRecord::Base.map method for direct select by single column
Diffstat (limited to 'activerecord/lib/active_record/relation/calculations.rb')
-rw-r--r-- | activerecord/lib/active_record/relation/calculations.rb | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index af86771d2d..0f57e9831d 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -166,6 +166,23 @@ module ActiveRecord 0 end + # 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. + # + # Examples: + # + # Person.pluck(:id) # SELECT people.id FROM people + # Person.uniq.pluck(:role) # SELECT DISTINCT role FROM people + # 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)) + end + end + private def perform_calculation(operation, column_name, options = {}) |