For my current side-project, I need to send emails to a list of people whose email addresses are stored in the database. My concern, as you can imagine, is to prevent spamming my users in instances of error or otherwise. There’s several ways to do this. As usual, it comes down to a choice - a judgement call from among the many choices.
- Creating an emails model to keep track of when an email was sent and to whom.
- Storing the last time a user received an email in the user model.
You can think of many others ways to do this of course.
I decided to go with the second option as it required minimal effort and has so far turned out
very well. This was accomplished with a single
rails generate migration AddLastEmailToSubscribers last_email:datetime.
This will create the following migration
1 2 3 4 5
From here, I can simply check if the :last_email is nil or a day before today’s,
and if so, then send the day’s email, and then update the last_email to