1

I am writing a rake task, to populate a "Product" object records. 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. How can I change the above query?

Suneel Kumar
  • 1,650
  • 2
  • 21
  • 31
John
  • 1,273
  • 3
  • 27
  • 61

2 Answers2

2

Ref this

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

UserProduct.where('created_at <= ?', Time.now - 90.days ).find_each(batch_size: 10000) do |user_product|
  user_product.save
end
Salil
  • 46,566
  • 21
  • 122
  • 156
1

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.

hoangdd
  • 511
  • 2
  • 13
  • is it less than or equal to or greater than or equal to? because i am getting all the records. – John Feb 05 '18 at 10:31
  • Sorry, Its my mistake. I edited answer to `created_at` greater than or equal to 90 days back from now. – hoangdd Feb 05 '18 at 10:39