方法
- B
- D
- E
- U
包含的模块
属性
[R] | finish | 主键值,其中 |
[R] | relation | 关系,其中 |
[R] | start | 主键值,其中 |
实例公共方法
batch_size() 链接
由 BatchEnumerator
产生的批次的大小。
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/relation/batches/batch_enumerator.rb, line 27 def batch_size @of end
delete_all() 链接
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/relation/batches/batch_enumerator.rb, line 65 def delete_all sum(&:delete_all) end
destroy_all() 链接
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/relation/batches/batch_enumerator.rb, line 85 def destroy_all each(&:destroy_all) 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 94 def each(&block) enum = @relation.to_enum(:in_batches, of: @of, start: @start, finish: @finish, load: false, 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
提供一个块,它将返回一个枚举器,以便与其他方法链接
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 52 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, order: @order).each do |relation| relation.records.each(&block) 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 74 def update_all(updates) sum do |relation| relation.update_all(updates) end end