ruby-on-railsrubyactiverecordbatch-query

Ordered batches clear solution


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.


Solution

  • Straight from the docs:

    NOTE: It’s not possible to set the order. That is automatically set to ascending on the primary key (“id ASC”) to make the batch ordering work. This also means that this method only works when the primary key is orderable (e.g. an integer or string).

    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: :desc you 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