Class: ActiveRecord::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/freedom_patches/fast_pluck.rb,
lib/freedom_patches/pluck_first.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

#pluck_first(*attributes) ⇒ Object



4
5
6
7
# File 'lib/freedom_patches/pluck_first.rb', line 4

def pluck_first(*attributes)
  Discourse.deprecate("`#pluck_first` is deprecated, use `#pick` instead.")
  pick(*attributes)
end

#pluck_first!(*attributes) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/freedom_patches/pluck_first.rb', line 9

def pluck_first!(*attributes)
  Discourse.deprecate("`#pluck_first!` is deprecated without replacement.")
  items = pick(*attributes)

  raise_record_not_found_exception! if items.nil?

  items
end