Chris Hall
2007-Feb-27 18:38 UTC
how to pass info external to model to after_create callback
I''ve got a situation where i have a model that whenever something changes i want to drop a row in another table (audits) to indicate that the record changed and who changed it. currently whenever i save the data i make a call to the models audit method and pass in a User object. keep in mind that the model i am auditing does not reference a user, which is why i have to pass in the User object to the audit call. anyways, i want to add after_create and before destroy callbacks to the model but I am hung up on how to pass the User object via the callback. Chris --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Aaron
2007-Feb-27 19:56 UTC
Re: how to pass info external to model to after_create callback
Chris, Take a look at the documentation on observers: http://api.rubyonrails.org/classes/ActiveRecord/Observer.html You may find this helpful, especially if you want to have an audit trail on more than one table. You can pass the user to the model using Thread.current: Thread.current[:user] = your_user The Rails Recipes book has an audit trail recipe ("Keeping Track of Who Did What") that uses a cache sweeper that may give you some ideas. Aaron --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---