Nadal
2010-May-12 02:15 UTC
How to add an after_save callback on runtime on a particular instance
class User < AR end user = User.new Let''s say that I have a user instance with me. After this record is saved I want an after_save callback which would print the id of the record. The only catch is that I am not allowed to change the User class. How can I accomplish my goal? Is that even possible to add an after_save callback on a particular instance object. Any metaprogramming hack. I''m sure ruby does provide some tool. This would be a trivial issue if I were allowed to change the User class but I am not. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2010-May-12 08:23 UTC
Re: How to add an after_save callback on runtime on a particular instance
On 12 May 2010 03:15, Nadal <node.js99-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> class User < AR > end > > user = User.new > > > Let''s say that I have a user instance with me. After this record is > saved I want an after_save callback which would print the id of the > record. > The only catch is that I am not allowed to change the User class. How > can I accomplish my goal? Is that even possible to add an after_save > callback on a particular instance object. Any metaprogramming hack. > I''m sure ruby does provide some tool. > > > This would be a trivial issue if I were allowed to change the User > class but I am not.What do you mean by ''not allowed to change the User class''? Do you mean that you are not allowed to change the file user.rb? If so that is not a problem as you can modify or extend the class in another loaded file merely by providing class User < ... and providing new or modified methods there. Colin> > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-May-12 08:51 UTC
Re: How to add an after_save callback on runtime on a particular instance
On May 12, 3:15 am, Nadal <node.j...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> class User < AR > end > > user = User.new > > Let''s say that I have a user instance with me. After this record is > saved I want an after_save callback which would print the id of the > record. > The only catch is that I am not allowed to change the User class. How > can I accomplish my goal? Is that even possible to add an after_save > callback on a particular instance object. Any metaprogramming hack. > I''m sure ruby does provide some tool.well on top of what Colin says you can do def user.some_method end or user.extend SomeModule I think both will prevent you from marshalling the object and I seem to remember that in MRI both will cause ruby to dump it''s cache of method names to implementations Fred> > This would be a trivial issue if I were allowed to change the User > class but I am not. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.