Ruby on rails 如何在rails中针对特定条件批量获取记录?
我正在编写一个rake任务,以填充“产品”对象记录。我目前的逻辑是Ruby on rails 如何在rails中针对特定条件批量获取记录?,ruby-on-rails,datecreated,Ruby On Rails,Datecreated,我正在编写一个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
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天前创建的记录,从现在开始。如何更改上述查询?Ref
差不多
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您可以尝试:
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
希望这有帮助。你是说正好90天以前?或者最多90天以前?或者至少90天以前?正好90天以前。你使用的Rails版本是什么?它是否小于或等于或大于或等于?因为我正在获取所有记录。抱歉,这是我的错误。我编辑了在
大于或等于90时创建的\u的答案几天过去了。