Skip to content

Instantly share code, notes, and snippets.

@kyprifog
Created October 6, 2015 20:41
Show Gist options
  • Save kyprifog/df2d902d18c0b1c0c6c0 to your computer and use it in GitHub Desktop.
Save kyprifog/df2d902d18c0b1c0c6c0 to your computer and use it in GitHub Desktop.
Active Record Pluck All
# 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