This is an example implementation of find_each
with ActiveResource
- has_scope
- rails (> 3)
# API side | |
# app/controllers/cities_controller.rb | |
class CitiesController | |
has_scope :limit | |
has_scope :offset | |
def index | |
@cities = apply_scopes(City).all | |
end | |
end |
# Client side | |
# app/models/city.rb | |
class City < ActiveResource::Base | |
self.site = "http://api.example.com" | |
include Concerns::Batches | |
end |
# Client side | |
# app/models/concerns/batches.rb | |
module Concerns | |
module Batches | |
extend ActiveSupport::Concern | |
module ClassMethods | |
def find_in_batches(options={}) | |
start = options.delete(:start).to_i | |
batch_size = options.delete(:batch_size) || 1000 | |
begin | |
records = find(:all, params: { offset: start, limit: batch_size }) | |
records_size = records.size | |
start += batch_size | |
yield records | |
break if records_size < batch_size | |
end while records.any? | |
end | |
def find_each(options={}) | |
find_in_batches(options) do |records| | |
records.each { |record| yield record } | |
end | |
end | |
end | |
end | |
end |