Skip to content

Instantly share code, notes, and snippets.

@zechtz
Created September 14, 2015 12:12
Show Gist options
  • Save zechtz/eef940ee8f1e9bbd4322 to your computer and use it in GitHub Desktop.
Save zechtz/eef940ee8f1e9bbd4322 to your computer and use it in GitHub Desktop.
Pluck Multiple Columns Rails 3
module ActiveRecord
class Relation
def pluck_multiple(*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
@zechtz
Copy link
Author

zechtz commented Sep 14, 2015

Add this file in intitializers directory

in console

User.where('sign_in_count > 0').pluck_multiple(:sign_in_count, :email)

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