views:

164

answers:

1

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!

+2  A: 

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.

wesgarrison