Last active
December 16, 2015 20:19
-
-
Save aliang/5492039 to your computer and use it in GitHub Desktop.
helper to iterate all by pagination using kaminari
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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