[英]Rails - how to fetch from ActiveRecord object only specific records?
[英]How to fetch records in batches in rails for a specific condition?
我正在编写一个rake任务,以填充“产品”对象记录。 我目前的逻辑是
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
现在在上面的查询中,我想获取从现在开始90天之前创建的记录。 如何更改以上查询?
引用此
就像是
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
对于90天后创建的记录
UserProduct.where('created_at <= ?', Time.now - 90.days ).find_each(batch_size: 10000) do |user_product|
user_product.save
end
你可以试试:
UserProduct.where('created_at >= ?', Time.now - 90.days ).find_each(batch_size: 10_000) do |user_product|
user_product.save
end
注意:如果您不关心小时和分钟,可以使用:
Date.today - 90.days
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.