Rails Mailer Best Practice - controller or after_create callback? [closed] Rails Mailer Best Practice - controller or after_create callback? [closed] ruby-on-rails ruby-on-rails

Rails Mailer Best Practice - controller or after_create callback? [closed]


Generally, it's much more difficult to maintain code that has after_* hooks in models. There are, of course some cases, when using a callback is very reasonable (computing a checksum, for instance, should be done all the time in some applications), but those cases are exceptions to the rule.

In your example with the email, here's some drawbacks of the callback approach:

The mailer interface and the Listing model change for different reasons (Single Responsibility Principle).

For instance, you would like your emails to be sent using a special queue. The interface of communicating with the queue should not, in any way, affect the way the Listing model is built.

Your application doesn't need emailing all the time.

Emailing is just one of the means of interacting with the outside world. Not all of the business logic will always require to be tied to the outside world. One of the examples, as apneadiving mentioned is import. Another example is the console interface (would you like to be sent an email when you're playing with the rails console?)

Testing is difficult.

This is more of a result of 1 and 2, but testing after hooks is increasingly difficult as time passes. The need of mocking the mailer while testing the Listing model makes the test unclear and more difficult to maintain whenever something changes.