I''m thinking about using observers for my situation. Basically whenever an object is created, I want a message to be sent to a user via a PM system I made. At the moment this is happening within the create method of the controller. I tried putting this in a callback in the model, but it seems methods such as the URLs (users_path) and current_user don''t work in models. Since observers are also a type of model, I was wondering if these methods don''t work in them either? --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
you can include them in the model if you want. Mybe i get some screaming for this as models and views should be completely separate and independent (so if you change the routes, for example, you would have to change this method, for you its not a problem because you worte it, but in a project with more people, it would net be a normal thing to do. just include ActionView::Helpers::UrlHelper. and you can get methods like url_for. I would defenitely put such method in a callback in the model: after_create :announce On Mar 14, 12:21 am, Mike C <snib...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m thinking about using observers for my situation. Basically > whenever an object is created, I want a message to be sent to a user > via a PM system I made. At the moment this is happening within the > create method of the controller. I tried putting this in a callback in > the model, but it seems methods such as the URLs (users_path) and > current_user don''t work in models. Since observers are also a type of > model, I was wondering if these methods don''t work in them either?--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
If what I want to accomplish affects only the model with which I''m working, I put the behavior in a callback on the model. If it affects other related models, I put it in an observer. Otherwise, I put it in the controller or some other non-ActiveRecord library code. In those cases where I''ve needed a URL, either I build and use it in the controller, or I build it in the controller and pass it to the thing that will do the work, e.g., a mailer or other notifier. One reason I wouldn''t put notification code in an observer is that it will fire no matter how you create the model: via the normal flow through the Rails stack, in script/console, etc. I usually only want the notification if the work is being done through the web app, not script/console. Food for thought. Regards, Craig -- Craig Demyanovich Mutually Human Software http://mutuallyhuman.com --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Oh, ok. Thanks for the replies! On Mar 16, 8:17 am, Craig Demyanovich <cdemyanov...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> If what I want to accomplish affects only the model with which I''m working, > I put the behavior in a callback on the model. If it affects other related > models, I put it in an observer. Otherwise, I put it in the controller or > some other non-ActiveRecord library code. In those cases where I''ve needed a > URL, either I build and use it in the controller, or I build it in the > controller and pass it to the thing that will do the work, e.g., a mailer or > other notifier. > > One reason I wouldn''t put notification code in an observer is that it will > fire no matter how you create the model: via the normal flow through the > Rails stack, in script/console, etc. I usually only want the notification if > the work is being done through the web app, not script/console. > > Food for thought. > > Regards, > Craig > > -- > Craig Demyanovich > Mutually Human Softwarehttp://mutuallyhuman.com--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---