ActiveRecord find_each combined with limit and order

后端 未结 13 2111
温柔的废话
温柔的废话 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:27

    Retrieving the ids first and processing the in_groups_of

    ordered_photo_ids = Photo.order(likes_count: :desc).pluck(:id)
    
    ordered_photo_ids.in_groups_of(1000, false).each do |photo_ids|
      photos = Photo.order(likes_count: :desc).where(id: photo_ids)
    
      # ...
    end
    

    It's important to also add the ORDER BY query to the inner call.

提交回复
热议问题