On Aug 6, 10:43 am, TomRossi7 <[email protected]> wrote:
> Should models call action mailers, or should those calls always
> originate from controllers?  For example, should user.forgot_password
> send the email, or should the user_controller.forgot_password?
>
> Just looking for some opinions...

Consider the questions this way: Are there occasions when models
should call models directly? Should all model interaction only be
mediated by an external controller?

The answers are clearly Yes in the first instance and No in the
second.  Consider the entire idea of associations for just one example
of model to model calls without discrete controllers being necessary
or desirable.

The names that you have given your examples somewhat muddy the
situation, however.  #forgot_password is, in my opinion, a controller
type action since it handles an external state, only the user can say
whether or not they have forgotten their password, which only the user
can communicate to the application.  However, #email_password is more
likely than not a model method since the email address and password
are bound to the user instance regardless of whether the user has
forgotten their password or not.  The fact that #email_password is an
action rather than simply state is besides the point.  Models may have
lots of actions.

In the first case one is specifying when to act, whilst in the second
what to do.  "When" is a question I believe best answered in a
controller, whether interactive or batch.  "What" is a question that
really is bound to the model that provides it.  So, in my
conceptualisation of this situation, users_controller.forgot_password
would look something like this:

def forgot_password
  @current_user.email_password
end

Now, as for the matter of ALWAYS.  This issue rather turns on the
nature of the request I should think.  If one is speaking of a single
instance, as in the case of @current_user, then the approach taken
above seems suitable.  But, what of the case when we are dealing with
collections?

Say the issue was who to transmit a personalised newsletter to, given
a set of criteria.  In this case, does it make sense to use a model
method to send the email or should a controller mediate between the
set of qualified addressees and the message to send?  I should think
that an external controller seems more appropriate here, as the
implementation of such a feature in the model would in all likelihood
require a class method that would probably start to look an awful lot
like a controller method.

IMHO of course.



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to