Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionMailer best practices: Call method in the model or the controller?

Tags:

Sending an email is usually called after an action on a model, but the email itself is a view operation. I'm looking for how you think about what question(s) to ask yourself to determine where to put the action mailer method call.

I've seen/used them:

  • In a model method - bad coupling of related but seperate concerns?
  • In a callback in the model (such as after_save) - best separation as far as I can tell with my current level of knowledge.
  • In the controller action - just feels wrong, but are there situations were this would be the smartest way to structure the code?

If I want to know how to program I need to think like a programmer, so learning how you go about thinking through particular programming solutions is worth months of coding on my own in isolation. Thank you!

like image 237
inkdeep Avatar asked Feb 03 '09 19:02

inkdeep


People also ask

What is ActionMailer?

Action Mailer is the Rails component that enables applications to send and receive emails. In this chapter, we will see how to send an email using Rails. Let's start creating an emails project using the following command. tp> rails new mailtest. This will create the required framework to proceed.

What generates information on the mailing run if available?

rb, production. rb, etc...) Generates information on the mailing run if available.


2 Answers

Late answer, but I want to rationalize on the subject:

Usually, in a web app, you want to send emails either as a direct reaction to a client. Or as a background task, in case we're talking about a newsletter/notification mail sort of thing.

The model is basically a data storage mapper. Its logic should encapsulate data-handling/communication with data storage handling. Therefore, inserting logic which does not relate to it is a bit tricky, and in most cases wrong. Let us take the example: User registers an account and should receive a confirmation email. In this case one could say, the confirmation email is a direct effect of the creation of a new account. Now, instead of doing it in the web app, try to create a user in the console. Sounds wrong to trigger a callback in that case, right? So, callback option scratched. Should we still write the method in the model? Well, if it's a direct effect of a user action/input, then it should stay in that workflow. I would write it in the controller after the user was successfully created. Directly. Replicating this logic in the model to be called in the controller anyways adds unnecessary modularity, and dependency of an Active Record model from Action Mailer. Try to consider sharing the model over many apps, in which some of them don't want Action Mailer for it. For the stated reasons, I'm of the opinion that the mailer calls should be where they make sense, and usually the model is not that place. Try to give me examples where it does make.

like image 199
ChuckE Avatar answered Oct 27 '22 14:10

ChuckE


Well, depends.

I've used all of those options and your point about 'why should I put this where?' is good.

If it's something I want to happen every time a model is updated in a certain way, then I put it in the model. Maybe even in a callback in the model.

Sometimes you're just firing off a report; there's no updating of anything. In that case, I've normally got a resource with an index action that sends the report.

If the mailer isn't really related to the model that's being changed, I could see putting it in a callback. I don't do that very often. I'd be more likely to still encapsulate it in the model. I've done it, just not very often.

like image 21
wesgarrison Avatar answered Oct 27 '22 13:10

wesgarrison