LoginSignup
73

More than 5 years have passed since last update.

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 = {})というのもあります。

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
73