views:

83

answers:

3

I have an Event model, which stores an event feed for each user. I also need to email the updates to users which have enabled email notifications in their profile.

From an architectural point of view, which one is better?

  1. call the mailer in an after_create method in the model, since it's a part of the business logic;
  2. call the mailer in an after_create method in an observer, since it doesn't really operate on the model and it also depends on the user's settings.
A: 

There's no "magic" answer to that as it's subjective.
Some developers will prefer to do that directly in the model. Others will do it in an observer.
What you do is completely up to you. Both solutions are valid.

I personnaly prefer to add them in an observer as it allows me to have all the user notifications in the same place and not one in one model and an other in an other model.

Damien MATHIEU
+1  A: 

I'd go with an observer. My reason for this is that sending a notification email isn't an essential part of the business logic of an event (put simply, events don't send notifications). Other than that, as Damien has already said, it's a matter of separation of concerns.

Please, see the discussion here, as well.

Milan Novota
A: 

The observer approach will be more flexible. If your email setting (addressee or server) changes, or the criteria for notification (daily to weekly or vice versa), it is a simpler matter to change the observer than it is to modify the model.

Kelly French