Does anyone know how I would get access to the request.env object within a model observer? I''m using restful_authentication to send email, and need to include some of that request data in there. I have it working with normal ActionMailer stuff, but can''t figure out how to do it in an observer. Thanks, Wade --~--~---------~--~----~------------~-------~--~----~ 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''t. Request information is related to controllers and views and should not be made available to models. Look at Restful authentication and see how it handles that - they use attr_accessor to store data. This is a really dumbed-down approach but you could do this: class ModelObserved < AR::Base attr_accessor :request_info # rest of your code end class ModelObservedObserver < AR::Observer observes ModelObserved def after_save(record) unless record.request_info.blank? # do stuff with this end end end Then in your controller you just need to make sure you pass the request.env stuff to the model. The other way to do it is to use cache_sweepers instead of observers. Acts_as_audited does that so you might learn something from that code. On Thu, Jul 17, 2008 at 1:03 PM, H. Wade Minter <minter-VaDJcK/n3l4h9ZMKESR00Q@public.gmane.org> wrote:> > Does anyone know how I would get access to the request.env object > within a model observer? I''m using restful_authentication to send > email, and need to include some of that request data in there. > > I have it working with normal ActionMailer stuff, but can''t figure out > how to do it in an observer. > > Thanks, > Wade > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
H. Wade Minter
2008-Jul-17 19:12 UTC
Re: Getting access to request.env in a model observer?
Thanks - I used an attr_accessor and it seems to do the trick! --Wade On Jul 17, 2008, at 1:08 PM, Brian Hogan wrote:> You can''t. Request information is related to controllers and views > and should not be made available to models. > > Look at Restful authentication and see how it handles that - they > use attr_accessor to store data. > > > This is a really dumbed-down approach but you could do this: > > class ModelObserved < AR::Base > > attr_accessor :request_info > > # rest of your code > end > > > > class ModelObservedObserver < AR::Observer > > observes ModelObserved > > def after_save(record) > unless record.request_info.blank? > # do stuff with this > end > end > > > end > > Then in your controller you just need to make sure you pass the > request.env stuff to the model. > > > The other way to do it is to use cache_sweepers instead of > observers. Acts_as_audited does that so you might learn something > from that code. > > > On Thu, Jul 17, 2008 at 1:03 PM, H. Wade Minter > <minter-VaDJcK/n3l4h9ZMKESR00Q@public.gmane.org> wrote: > > Does anyone know how I would get access to the request.env object > within a model observer? I''m using restful_authentication to send > email, and need to include some of that request data in there. > > I have it working with normal ActionMailer stuff, but can''t figure out > how to do it in an observer. > > Thanks, > Wade > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---