Class: ActiveRecord::Relation
- Inherits:
-
Object
- Object
- ActiveRecord::Relation
- Defined in:
- lib/freedom_patches/fast_pluck.rb
Overview
Speeds up #pluck so its about 2.2x faster, importantly makes pluck avoid creation of a slew of AR objects
Instance Method Summary collapse
Instance Method Details
#pluck(*column_names) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/freedom_patches/fast_pluck.rb', line 44 def pluck(*column_names) if loaded? && (column_names.map(&:to_s) - @klass.attribute_names - @klass.attribute_aliases.keys).empty? return records.pluck(*column_names) end if has_include?(column_names.first) relation = apply_join_dependency relation.pluck(*column_names) else relation = spawn relation.select_values = column_names klass .connection .select_raw(relation.arel) do |result, _| result.type_map = DB.type_map result.nfields == 1 ? result.column_values(0) : result.values end end end |