Toshi
Toshi

Reputation: 6342

ActionMailer Template stored in DB

The question here is

"Is it bad practice to store email templates in DB and render?"

I know this is what people don't apply. (Usually it's rendered from .erb and good to go, and it should be avoided from accessing DB as much as possible.)

But there are business requirements below

Third party Transaction Email service provider can store the histories, but I need to store them locally.

So I'm thinking about storing templates within DB, and render it, so that I can track what email was sent even templates change.

Any advices for that?

Upvotes: 0

Views: 442

Answers (1)

Andrey Deineko
Andrey Deineko

Reputation: 52347

For business reasons, I need to have the history for each emails. (Who's got sent, what title and bodies are sent.)

You can go with the following approach:

  1. create the db-backed AR model, like EmailsHistory
  2. create instance of EmailsHistory each time the email is coming out.

## Columns:
#  * title
#  * recipient
#  * body
#  * anything else
#
class EmailsHistory
end

Then, at the moment of email sending just add a row to emails_history table with all the columns you need to have info of.

Upvotes: 1

Related Questions