Question: Check if user has restaurant 48hours after sign up

Question

Check if user has restaurant 48hours after sign up

Answers 1
Added at 2017-09-07 20:09
Tags
Question

I have some trouble to check if my user have created a restaurant 48 hours after signed_up ( the objective is to send an Email reminder).

I tried differents things and right now i'm with this in my

USER MODEL : User.rb

after_create :reminder



def reminder
    if Time.now.utc == self.created_at + 48.hours && self.restaurants.count == 0
    UserMailer.theactionofthemail(self).deliver_later
 end

I tried it like this because i saw that with sidekiq i didn't have to create job to deliver_later my UserMailers .. :)

Any Clues ? If it's not clear tell me !

Thank you in Advance

Answers to

Check if user has restaurant 48hours after sign up

nr: #1 dodano: 2017-09-07 21:09

after_create will call that method immediately after create, so created_at will never be equal (BTW, usually you want to say >=, just in case milliseconds have passed) to the users's created_at.

Instad I suggest you explore ActiveJob. Create a job that does this same question, except is ran 48 hours later:

after_create :reminder

def reminder
  CreateRestaurantReminderJob.set(wait: 48.hours).perform_later self
end

In that job:

def perform(user)
  return unless user.restaurants.count.zero?
  UserMailer.theactionofthemail(user).deliver_now
end

If you are already using Sidekiq, you can configure it as the backend for ActiveJob, or use it's own workers. The syntax might change, but the same concept stands.

Source Show
◀ Wstecz