Module: ActiveRecord::Collections::Records
- Included in:
- ActiveRecord::Collection
- Defined in:
- lib/active_record/collections/records.rb
Instance Method Summary collapse
- #each(&block) ⇒ Object
- #flat_map(&block) ⇒ Object
- #length ⇒ Object
- #map(&block) ⇒ Object
- #pluck(col) ⇒ Object
- #record_ids ⇒ Object
- #records ⇒ Object
- #size ⇒ Object
- #to_ary ⇒ Object (also: #to_a)
- #total_count ⇒ Object (also: #total, #count)
- #total_records ⇒ Object
Instance Method Details
#each(&block) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/active_record/collections/records.rb', line 40 def each(&block) batch! if try(:should_batch?) if try(:batched?) flat_batch_map.each { |record| block_given? ? yield(record) : record } else records.each { |record| block_given? ? yield(record) : record } end end |
#flat_map(&block) ⇒ Object
60 61 62 |
# File 'lib/active_record/collections/records.rb', line 60 def flat_map(&block) map(&block).flatten end |
#length ⇒ Object
36 37 38 |
# File 'lib/active_record/collections/records.rb', line 36 def length to_a.length end |
#map(&block) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/active_record/collections/records.rb', line 50 def map(&block) batch! if try(:should_batch?) if try(:batched?) flat_batch_map.map { |record| block_given? ? yield(record) : record } else each.map { |record| block_given? ? yield(record) : record } end end |
#pluck(col) ⇒ Object
12 13 14 |
# File 'lib/active_record/collections/records.rb', line 12 def pluck(col) relation.pluck(col) end |
#record_ids ⇒ Object
8 9 10 |
# File 'lib/active_record/collections/records.rb', line 8 def record_ids @record_ids ||= records.loaded? ? records.map(&:id) : records.pluck(:id) end |
#records ⇒ Object
4 5 6 |
# File 'lib/active_record/collections/records.rb', line 4 def records @records ||= relation end |
#size ⇒ Object
32 33 34 |
# File 'lib/active_record/collections/records.rb', line 32 def size @size ||= relation.size end |
#to_ary ⇒ Object Also known as: to_a
16 17 18 |
# File 'lib/active_record/collections/records.rb', line 16 def to_ary records.to_a end |
#total_count ⇒ Object Also known as: total, count
25 26 27 28 |
# File 'lib/active_record/collections/records.rb', line 25 def total_count #batch! if try(:should_batch?) total_records end |
#total_records ⇒ Object
21 22 23 |
# File 'lib/active_record/collections/records.rb', line 21 def total_records @total_records ||= relation.limit(nil).count end |