Created
September 7, 2012 19:56
-
-
Save amcclosky/3669131 to your computer and use it in GitHub Desktop.
An Extension to ActiveRecord::Relation to add a values method for pulling data outside of ActiveRecord::Models
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
module ActiveRecord | |
module RawValues | |
def values(options={}) | |
relation = self | |
select_values = options[:select] | |
flat_result = options[:flat] | |
relation.select_values = select_values if select_values | |
query = relation.build_arel | |
if !flat_result | |
result = connection.send(:select_all, query) | |
result = result.map(&:symbolize_keys!) | |
else | |
result = connection.send(:select_values, query) | |
end | |
return result | |
end | |
end | |
class Relation | |
include RawValues | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment