方法
- B
- D
- E
- T
- U
包含的模块
Attributes
| [R] | finish |
|
| [R] | relation |
|
| [R] | start |
|
实例公共方法
batch_size() 链接
BatchEnumerator 产生的批次的大小。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/relation/batches/batch_enumerator.rb, line 28 def batch_size @of end
delete_all() 链接
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/relation/batches/batch_enumerator.rb, line 66 def delete_all sum(&:delete_all) end
destroy_all() 链接
分批销毁记录。返回受影响的总行数。
Person.where("age < 10").in_batches.destroy_all
有关如何销毁每个批次的详细信息,请参阅 Relation#destroy_all。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/relation/batches/batch_enumerator.rb, line 97 def destroy_all sum do |relation| relation.destroy_all.count(&:destroyed?) end end
each(&block) 链接
为每个记录批次产生一个 ActiveRecord::Relation 对象。
Person.in_batches.each do |relation| relation.update_all(awesome: true) end
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/relation/batches/batch_enumerator.rb, line 108 def each(&block) enum = @relation.to_enum(:in_batches, of: @of, start: @start, finish: @finish, load: false, cursor: @cursor, order: @order, use_ranges: @use_ranges) return enum.each(&block) if block_given? enum end
each_record(&block) 链接
遍历数据库中的记录集合(例如,使用 all 方法)效率非常低,因为它会尝试一次性实例化所有对象。
在这种情况下,分批处理方法允许您分批处理记录,从而大大降低内存消耗。
Person.in_batches.each_record do |person| person.do_awesome_stuff end Person.where("age > 21").in_batches(of: 10).each_record do |person| person.party_all_night! end
如果您不为 each_record 提供块,它将返回一个 Enumerator,用于与其他方法链式调用。
Person.in_batches.each_record.with_index do |person, index| person.award_trophy(index + 1) end
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/relation/batches/batch_enumerator.rb, line 53 def each_record(&block) return to_enum(:each_record) unless block_given? @relation.to_enum(:in_batches, of: @of, start: @start, finish: @finish, load: true, cursor: @cursor, order: @order).each do |relation| relation.records.each(&block) end end
touch_all(...) 链接
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/relation/batches/batch_enumerator.rb, line 86 def touch_all(...) sum do |relation| relation.touch_all(...) end end
update_all(updates) 链接
分批更新记录。返回受影响的总行数。
Person.in_batches.update_all("age = age + 1")
有关如何更新每个批次的详细信息,请参阅 Relation#update_all。
来源: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/relation/batches/batch_enumerator.rb, line 75 def update_all(updates) sum do |relation| relation.update_all(updates) end end