-
as described above, I need to batch process some data in a static method. I originally wanted to use class DataService
include Pagy::Backend
class << self
def change_title(options = {})
records_relation = Product.where(created_at: (Time.zone.now - 10.days)..Time.zone.now).order(group_id: :desc)
pagy, records = pagy(records_relation, page: 1, items: 30)
(1..pagy.pages).each do |page|
_, records = pagy(records_relation, page: page, items: 30)
records.each do |record|
record.update(title: "new title")
end
end
end
end
end As shown in the above code, the pagy method is not working properly. What should I do? Thank you so much. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
That does not explain anything useful to help you. BTW, it looks like you are using pagy to update records... which it's like using a bazooka to kill a fly. You just need to loop through your collection in chunks of n records at the time. You should not use pagy to do that. There are many different ways to do that: an easy one is using |
Beta Was this translation helpful? Give feedback.
I missed that part
It's less efficient, but you can loop through batches anyway if you do it by yourself, with OFFSET and LIMIT