Created
March 11, 2020 19:15
-
-
Save euclideansphere/268b5739a953fd4631c35b6eff223401 to your computer and use it in GitHub Desktop.
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
# 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