ActiveRecord find_each combined with limit and order

后端 未结 13 2103
温柔的废话
温柔的废话 2020-12-02 11:41

I\'m trying to run a query of about 50,000 records using ActiveRecord\'s find_each method, but it seems to be ignoring my other parameters like so:



        
13条回答
  •  一向
    一向 (楼主)
    2020-12-02 12:43

    Adding find_in_batches_with_order did solve my usecase, where I was having ids already but need batching and ordering. It was inspired by @dirk-geurs solution

    # Create file config/initializers/find_in_batches_with_order.rb with follwing code.
    ActiveRecord::Batches.class_eval do
      ## Only flat order structure is supported now
      ## example: [:forename, :surname] is supported but [:forename, {surname: :asc}] is not supported
      def find_in_batches_with_order(ids: nil, order: [], batch_size: 1000)
        relation = self
        arrangement = order.dup
        index = order.find_index(:id)
    
        unless index
          arrangement.push(:id)
          index = arrangement.length - 1
        end
    
        ids ||= relation.order(*arrangement).pluck(*arrangement).map{ |tupple| tupple[index] }
        ids.each_slice(batch_size) do |chunk_ids|
          chunk_relation = relation.where(id: chunk_ids).order(*order)
          yield(chunk_relation)
        end
      end
    end
    

    Leaving Gist here https://gist.github.com/the-spectator/28b1176f98cc2f66e870755bb2334545

提交回复
热议问题