Skip to content

Instantly share code, notes, and snippets.

@euclideansphere
Created March 11, 2020 19:15
Show Gist options
  • Save euclideansphere/268b5739a953fd4631c35b6eff223401 to your computer and use it in GitHub Desktop.
Save euclideansphere/268b5739a953fd4631c35b6eff223401 to your computer and use it in GitHub Desktop.
# this works in Ruby 2.3 / Rails 4.2. Untested in other versions.
# ActiveRecord::Calculations.pluck implementation is essentially this with additional
# relation / scoping helpers (for chaining & type saftey) and caching logic.
def pluck(relation, *column_names)
relation.select_values = column_names
ActiveRecord::Base.connection.select_all(relation.arel).cast_values
end
# this totally sucks though, because if I want to pass Arel::Attributes or use Arel::SelectManager,
# I'm SOL.
# better, maybe?
def exec_to_array(relation)
ActiveRecord::Base.connection.select_all(relation).cast_values
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment