diff options
author | Ben Toews <mastahyeti@gmail.com> | 2017-02-21 11:17:16 -0700 |
---|---|---|
committer | Matthew Draper <matthew@trebex.net> | 2017-11-09 22:37:23 +1030 |
commit | 864b16063d14977096d9d24ac894fee605dfb7a7 (patch) | |
tree | a3741db56b6f3999ee984c00102ba7225fd92283 /activerecord/lib/active_record/relation/calculations.rb | |
parent | f989b341eccc6a86fd1ddfff7f1441920855c84e (diff) | |
download | rails-864b16063d14977096d9d24ac894fee605dfb7a7.tar.gz rails-864b16063d14977096d9d24ac894fee605dfb7a7.tar.bz2 rails-864b16063d14977096d9d24ac894fee605dfb7a7.zip |
allow Arel.sql() for pluck
Diffstat (limited to 'activerecord/lib/active_record/relation/calculations.rb')
-rw-r--r-- | activerecord/lib/active_record/relation/calculations.rb | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index dea7542f03..236d36e15f 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -175,13 +175,21 @@ module ActiveRecord # See also #ids. # def pluck(*column_names) - _pluck(column_names, @klass.allow_unsafe_raw_sql == :enabled) - end + if loaded? && (column_names.map(&:to_s) - @klass.attribute_names_and_aliases).empty? + return records.pluck(*column_names) + end - # Same as #pluck but allows raw SQL regardless of `allow_unsafe_raw_sql` - # config setting. - def unsafe_raw_pluck(*column_names) - _pluck(column_names, true) + if has_include?(column_names.first) + construct_relation_for_association_calculations.pluck(*column_names) + else + enforce_raw_sql_whitelist(column_names) + relation = spawn + relation.select_values = column_names.map { |cn| + @klass.respond_to_attribute?(cn) ? arel_attribute(cn) : cn + } + result = klass.connection.select_all(relation.arel, nil, bound_attributes) + result.cast_values(klass.attribute_types) + end end # Pluck all the ID's for the relation using the table's primary key |