LoginSignup
9
4

More than 5 years have passed since last update.

Rails5.1からlimit付きのfind_eachが使えるようになってた

Last updated at Posted at 2017-05-28

バッチ処理とかで大量データを効率的に処理したい時に使うfind_eachがあるんだけど、
Rails5.1.0より前まではlimitを付けてもレコードの数を制限することが出来なかった。

だけどRails5.1.0からはfind_eachにlimitを付けるとレコードの数を制限できるようになってた。

下記は例

# Userには200人登録されてるとする。
User.limit(100).find_each { |user| puts user }

# 5.1より前:200人分出力
# 5.1から :100人分出力

find_in_batches、in_batchesも同様にlimitで制限が掛けれるようになってる。

修正元はこちら
https://github.com/rails/rails/commit/451437c6f57e66cc7586ec966e530493927098c7

9
4
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
9
4