How can I send emails 30 days before the time limit 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!
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.
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:
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