Skip to content

Instantly share code, notes, and snippets.

@simonc
Created May 25, 2012 10:41
Show Gist options
  • Save simonc/2787243 to your computer and use it in GitHub Desktop.
Save simonc/2787243 to your computer and use it in GitHub Desktop.
find_each with ActiveResource

This is an example implementation of find_each with ActiveResource

Dependencies:

API:

  • has_scope

Client:

  • 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment