Skip to content

Instantly share code, notes, and snippets.

@aliang
Last active December 16, 2015 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aliang/5492039 to your computer and use it in GitHub Desktop.
Save aliang/5492039 to your computer and use it in GitHub Desktop.
helper to iterate all by pagination using kaminari
# Probably should investigate how to chain this with scopes
# Also assumes mongoid with a default _id field, and the order_by method
class MyModel
def self.each_by_page(options = {})
per = options[:per] || 25
order_by = options[:order_by] || [:_id, 1]
i = 1
until (current_page = self.order_by(order_by).page(i).per(per)).empty?
current_page.each do |obj|
yield obj
end
i = i + 1
end
end
end
MyModel.each_by_page(per: 1000) do |obj|
# ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment