69
73

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ruby on RailsAdvent Calendar 2012

Day 14

Railsで大量のレコードに対してeachしたい時のfind_each

Posted at

数千行とか数万行のレコードに対して何かしたい時にeachを使うとメモリがやばいときがあるでしょう、そんな時のためにfind_eachというのがあります。

Post.where(:deleted_at => nil).find_each do |post|
  puts post.subject
end

さらに、1レコードずつやってたら時間かかりすぎるわって場合は0~10000レコードぐらいずつ処理してくれるfind_in_batches(options = {}) というのもあります。

69
73
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
69
73

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?