Skip to content

Instantly share code, notes, and snippets.

@mmmpa
Created February 25, 2017 03:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmmpa/f04d5b870e8c04b2ac1615f05d33b0e4 to your computer and use it in GitHub Desktop.
Save mmmpa/f04d5b870e8c04b2ac1615f05d33b0e4 to your computer and use it in GitHub Desktop.
def pluck(*column_names)
if loaded? && (column_names.map(&:to_s) - @klass.attribute_names - @klass.attribute_aliases.keys).empty?
return records.pluck(*column_names)
end
if has_include?(column_names.first)
construct_relation_for_association_calculations.pluck(*column_names)
else
relation = spawn
relation.select_values = column_names.map { |cn|
@klass.has_attribute?(cn) || @klass.attribute_alias?(cn) ? arel_attribute(cn) : cn
}
result = klass.connection.select_all(relation.arel, nil, bound_attributes)
result.cast_values(klass.attribute_types)
end
end
@mmmpa
Copy link
Author

mmmpa commented Feb 25, 2017

activerecord/lib/active_record/relation/calculations.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment