Given the following code in ruby/rails, block code is omitted.
Model.order(id: :desc).find_in_batches { |group| ... }
If you run this, you will get a warning.
Scoped order and limit are ignored, it's forced to be batch order and batch size
Of course we can use some custom boilerplate code to achieve this, but I wondering is there rails-way?
I have found this, but I do not see the possibility to change it is behaviour. What if I don't need asc, but I need desc.
Straight from the docs:
The reason it is deliberately limited to primary_key order because those values don't change. So if you mutate the data as you're traversing it you dont get repeated options back.
In case of
id: :descyou will not get new records that were inserted after the transaction to get initial batch was started.Refs
https://rails.lighthouseapp.com/projects/8994/tickets/2502-patch-arbase-reverse-find_in_batches
https://ww.telent.net/2012/5/4/changing_sort_order_with_activerecord_find_in_batches
ActiveRecord find_each combined with limit and order