new on ruby and using windows xp and rails 3, i want to send emails to the users every 7 days for 34 weeks, i have usermailer that send emails every 7 days i want also for 34 weeks??
i have user mailer send emails to user, and i have defined a method to send emails every 7 days
and call it from a batch file using also a windows task scheduler
also i want to know which week is the user now in??
e.g user gets pregnant on April and singed up in June, then there will be a welcome message,then the first 8 weeks of her pregnancy wasn`t in our records that means the emails will start from week number 9 because 34 – 8 = 26 weeks so the user will receive 26 emails (1 email every 1 week).
my model:
class Dop < ActiveRecord::Base
attr_accessible :date, :mail, :name
validates_presence_of :name
validates_presence_of :mail
validates_uniqueness_of :mail
validates_format_of :mail, :with =>
%r{^(?:[_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-zA-Z0-9\-\.]
+)*(\.[a-z]{2,4})$}i
def self.send_mail
Dop.all.each do |dop|
UserMailer.welcome_email(dop).deliver if dop.date.to_time <= Time.now - 7.day
end
end
end
my batch file :
echo rails run
cd c:\dop
rails r "Dop.send_mail"
echo done
pause
Dop is refer to date of pregnancy.
Based on your environment, a pure ruby solution would look like this:
However, I prefer a more database solution. Wherein this gets more complicated, sqlite does not have the same date functions as MySql or Postgres. (Sqlite can be patched to keep your test/development environment like production, but I’m saving that for another question.)
Personally, the database solution (option 2), is the most elegant solution. Though it is more fragile, because it is specific to sqlite’s date functions.
Schedule this to run nightly and it will email every 7 days for 34 weeks.