1

我正在编写一个 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 天前创建的记录。如何更改上述查询?

4

2 回答 2

2

参考这个

就像是

 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
于 2018-02-05T10:08:29.393 回答
1

你可以试试:

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 

希望这可以帮助。

于 2018-02-05T10:09:52.003 回答