Created
October 6, 2015 20:41
-
-
Save kyprifog/df2d902d18c0b1c0c6c0 to your computer and use it in GitHub Desktop.
Active Record Pluck All
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pluck_all.rb | |
module ActiveRecord | |
class Relation | |
def pluck_all(*args) | |
args.map! do |column_name| | |
if column_name.is_a?(Symbol) && column_names.include?(column_name.to_s) | |
"#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column_name)}" | |
else | |
column_name.to_s | |
end | |
end | |
relation = clone | |
relation.select_values = args | |
klass.connection.select_all(relation.arel).map! do |attributes| | |
initialized_attributes = klass.initialize_attributes(attributes) | |
attributes.each do |key, attribute| | |
attributes[key] = klass.type_cast_attribute(key, initialized_attributes) | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment