Find record from a specific ID onwards
In Rails, I was running a rake task:开发者_如何转开发
@shops = Shops.find(:all)
@shops.each do |shop|
# some task here
end
I have 100 records. The task ran 1 to 50, and stopped because of some exception. How can I change my rake task to resume the task, but start running 51 onwards?
Thanks.
As requested in the comment, I'll post this as an answer:
@shops.sort_by(&:id).each[50..-1] do |shop|
# whatever you need to do
end
You can use with_index
@shops.each_with_index |shop, i|
# i will increment each time through the loop
end
Be aware that the index will start at 0. So to start with your 50th item you will want to start at index 49.
@shops.each do |shop|
begin
# some task here
rescue
# error
end
end
精彩评论