订购批次清除解决方案

时间:2019-06-27 08:29:03

标签: ruby-on-rails ruby activerecord batch-query

鉴于下面的代码在ruby / rails中,省略了块代码。

Model.order(id: :desc).find_in_batches { |group| ... }

如果运行此命令,则会收到警告。

Scoped order and limit are ignored, it's forced to be batch order and batch size

我们当然可以使用一些自定义样板代码来实现此目的,但是我想知道是否有导轨?

我找到了localized-app-name-for-xamarin-ios,但我看不出改变它的可能性。如果我不需要asc,但我需要desc,该怎么办。

1 个答案:

答案 0 :(得分:1)

直接从docs

  

注意:无法设置订单。即自动设置为   升序为主键(“ id ASC”)进行批量订购   工作。这也意味着该方法仅在主键   是可排序的(例如整数或字符串)。

有意将其限制在primary_key顺序中的原因是,这些值不会更改。因此,如果在遍历数据时对其进行变异,则不会得到重复的选择。

对于id: :desc,您将不会获得在开始获取初始批处理的事务之后插入的新记录。

参考

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