Search code examples
ruby-on-railsnamed-scope

Is there a way to get this scope to return results in batches


I have this scope. I would like to have it set up to return batches of 25.

scope :get_some_stuff, lambda {
                                select(QUERY_SELECT).
                                joins(QUERY_JOINS).
                                group(QUERY_GROUP_BY)
                          }

Solution

  • http://guides.rubyonrails.org/active_record_querying.html#retrieving-multiple-objects-in-batches

    Straight from the docs; untested, but I don't see why this wouldn't work.

    YourUnidentifiedModel.get_some_stuff.find_in_batches(batch_size: 25) do |batch|
      # Work with your batch
    end