activerecord/lib/active_record/relation/calculations.rb
def pluck(*column_names)
      column_names.map! do |column_name|
        if column_name.is_a?(Symbol) && self.column_names.include?(column_name.to_s)
          "#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column_name)}"
        else
          column_name
        end
      end

      if has_include?(column_names.first)
        construct_relation_for_association_calculations.pluck(*column_names)
      else
        relation = spawn
        relation.select_values = column_names
        result = klass.connection.select_all(relation.arel, nil, bind_values)
        columns = result.columns.map do |key|
          klass.column_types.fetch(key) {
            result.column_types.fetch(key) {
              Class.new { def type_cast(v); v; end }.new
            }
          }
        end

        result = result.map do |attributes|
          values = klass.initialize_attributes(attributes).values

          columns.zip(values).map do |column, value|
            column.type_cast(value)
          end
        end
        columns.one? ? result.map!(&:first) : result
      end
    end
# Use <tt>pluck</tt> as a shortcut to select one or more attributes without
# loading a bunch of records just to grab the attributes you want.
#
#   Person.pluck(:name)
#
# instead of
#
#   Person.all.map(&:name)
#
# Pluck returns an <tt>Array</tt> of attribute values type-casted to match
# the plucked column names, if they can be deduced. Plucking an SQL fragment
# returns String values by default.
#
#   Person.pluck(:id)
#   # SELECT people.id FROM people
#   # => [1, 2, 3]
#
#   Person.pluck(:id, :name)
#   # SELECT people.id, people.name FROM people
#   # => [[1, 'David'], [2, 'Jeremy'], [3, 'Jose']]
#
#   Person.pluck('DISTINCT role')
#   # SELECT DISTINCT role FROM people
#   # => ['admin', 'member', 'guest']
#
#   Person.where(age: 21).limit(5).pluck(:id)
#   # SELECT people.id FROM people WHERE people.age = 21 LIMIT 5
#   # => [2, 3]
#
#   Person.pluck('DATEDIFF(updated_at, created_at)')
#   # SELECT DATEDIFF(updated_at, created_at) FROM people
#   # => ['0', '27761', '173']
#
Use pluck as a shortcut to select one or more attributes without
loading a bunch of records just to grab the attributes you want.



instead of



Pluck returns an Array of attribute values type-casted to match
the plucked column names, if they can be deduced. Plucking an SQL fragment
returns String values by default.










Use pluck as a shortcut to select one or more attributes without loading a bunch of records just to grab the attributes you want . instead of Pluck returns an Array of attribute values type - casted to match the plucked column names if they can be deduced . Plucking an SQL fragment returns String values by default .
def pluck(*column_names)
      column_names.map! do |column_name|
        if column_name.is_a?(Symbol) && self.column_names.include?(column_name.to_s)
          "#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column_name)}"
        else
          column_name
        end
      end
      if has_include?(column_names.first)
        construct_relation_for_association_calculations.pluck(*column_names)
      else
        relation = spawn
        relation.select_values = column_names
        result = klass.connection.select_all(relation.arel, nil, bind_values)
        columns = result.columns.map do |key|
          klass.column_types.fetch(key) {
            result.column_types.fetch(key) {
              Class.new { def type_cast(v); v; end }.new
            }
          }
        end
        result = result.map do |attributes|
          values = klass.initialize_attributes(attributes).values
          columns.zip(values).map do |column, value|
            column.type_cast(value)
          end
        end
        columns.one? ? result.map!(&:first) : result
      end
    end
def pluck ( * column_names ) column_names . map! do | column_name | if column_name . is_a? ( Symbol ) && self . column_names . include? ( column_name . to_s ) "#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column_name)}" else column_name end end if has_include? ( column_names . first ) construct_relation_for_association_calculations . pluck ( * column_names ) else relation = spawn relation . select_values = column_names result = klass . connection . select_all ( relation . arel , nil , bind_values ) columns = result . columns . map do | key | klass . column_types . fetch ( key ) { result . column_types . fetch ( key ) { Class . new { def type_cast ( v ) ; v ; end } . new } } end result = result . map do | attributes | values = klass . initialize_attributes ( attributes ) . values columns . zip ( values ) . map do | column , value | column . type_cast ( value ) end end columns . one? ? result . map! ( & :first ) : result end end
Use <tt>pluck</tt> as a shortcut to select one or more attributes without
loading a bunch of records just to grab the attributes you want.
Use <tt > pluck< / tt > as a shortcut to select one or more attributes without loading a bunch of records just to grab the attributes you want .
column_names