Rails: is it better to send email notifications from a model or controller?

Suppose that items are sent for further processing (i.e., through screening or delayed_job).

What I want to know is the best practice of sending email notifications from ActiveController or ActiveRecord s?

I think itโ€™s useful to put it in the controller, since you can control whether you need to send an email message (i.e. after the model has saved some other criteria), but the idea that there is ONE place (model), sent by e-mail, if it is processed from the model after saving (create or update, etc.) is also useful.

Did any of you have this dilemma? What did you choose and why? Did you do something completely different?

+4
source share
2 answers

I personally like that everything is grouped in ActiveRecord. However, to send emails, I always do this from the controller. Imagine you are doing unit tests, or you are changing some kind of entry from the console. These are two situations that I encounter when I do not want to send emails.

+9
source

IMHO, rails is a flexible structure and, depending on how you start, you can always switch from model to controller and back with almost zero effort. In other words, this is simply not a problem, it is worth spending 2 hours.

But the important thing is that you all send the code in one place (usually in a subclass of ActionMailer::Base ). Thus, changing the place of sending email, you simply move one line of code.

+1
source

All Articles