[英]How to fetch records in batches in rails for a specific condition?
I am writing a rake task, to populate a "Product" object records. 我正在编写一个rake任务,以填充“产品”对象记录。 my current logic is
我目前的逻辑是
namespace :populate_product do
desc "This task is to populate product object, with product ID"
task populate_coaching_product_id: :environment do
UserProduct.find_each(batch_size: 10_000) do |user_product|
user_product.save
end
end
end
Now in the above query, I want to fetch records which are created 90 days back, from now. 现在在上面的查询中,我想获取从现在开始90天之前创建的记录。 How can I change the above query?
如何更改以上查询?
Something like 就像是
Person.where("age > 21").find_in_batches do |group|
sleep(50) # Make sure it doesn't get too crowded in there!
group.each { |person| person.party_all_night! }
end
For the records which are created 90 days back use 对于90天后创建的记录
UserProduct.where('created_at <= ?', Time.now - 90.days ).find_each(batch_size: 10000) do |user_product|
user_product.save
end
You can try: 你可以试试:
UserProduct.where('created_at >= ?', Time.now - 90.days ).find_each(batch_size: 10_000) do |user_product|
user_product.save
end
Note: If you don't care about hour & minute, you can use: 注意:如果您不关心小时和分钟,可以使用:
Date.today - 90.days
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.