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

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!
The easiest way is to run a cronjob to check the expiration date is in 30 days. If so, it sends an email. 
Let us know if you have specific issues implementing such thing.
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.

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


Nothing more to add to above post from Jacob! 🙂
Thank you very much both of you! love you all!
Join the discussion

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

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

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

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.