Alex Heil
Alex Heil

Reputation: 345

Send user email 24 hours after inactivity, rails

Basically, I want to send an email reminder to a User if they haven't made any posts within 24 hours of being created. I am confused on how to send an email without first making an action, such as create or update.

controller users_controller.rb

def run
  @user = current_user
  if Time.now.utc == @user.created_at + 24.hours && @user.microposts.empty?
    UserMailer.twentyfour_email(@user).deliver
  end
end

mailer user_mailer.rb

def twentyfour_mailer
  @user = user
  mail(to: @user.email, subject: 'Post something on the site!')
end

Upvotes: 0

Views: 681

Answers (2)

Long Nguyen
Long Nguyen

Reputation: 373

Make it easy.

You can use Delayed Job for instance.

UserMailer.twentyfour_mailer.delay(run_at: 24.hours.from_now)

Upvotes: 0

Anthony
Anthony

Reputation: 15967

You don't need this to happen via a controller action, you just need a cron job to fire off a task (the whenever gem does this). That might look like:

every 1.day, :at => '4:30 am' do
  runner "User.notify_lazy_users"
end

Upvotes: 2

Related Questions