Ask A Question

Notifications

You’re not receiving notifications from this thread.

How can I send emails 30 days before the time limit in rails?

Francisco Muñoz asked in Rails
I have a system that can send email every time I create a post, but I also need to notify 30 days before the expiration date through email, as you may know, the expiration date is variable and I could not find a way to solve this problem. Thank you in advance!
Reply
Yes, I have implemented cron jobs before, but the most complicated, I think, is the condition, the calculation that must be done to send emails 30 days before the expiration date, if there is an example or some documentation a little more detailed, I would appreciate if you could share it.

P.D .: I have used gems like delayed_jobs and whenever to use cron jobs, and I have implemented them well.
In conclusion, I would like to know which is the optimal and precise way to solve this problem, which is basically the condition to send the emails 30 days before the deadline, thank you very much for your response.

Reply
Hey Francisco, 

As Jack stated, you just need a cron job to check for records that expire in 30 days or less. So on your Post model you would store an expiration date or just check the created_at timestamp and then in your cron you could check for all records that have an expiration date that is less than 30 days from now with a scope. Eg:

scope :expires_in_30_days, -> {where("expiration_date < ?", Time.now + 30.days)}

Post.expires_in_30_days.find_each do |post|
  Post.send_expiration_email
end


Reply
Thank you very much both of you! love you all!
Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.