# How to add an after\_save callback on runtime on a particular instance

**URL:** <https://www.ruby-forum.com/t/how-to-add-an-after-save-callback-on-runtime-on-a-particular-instance/188445>\
**Category:** Rails\
**Created:** [May 12, 2010, 4:17am UTC](https://www.ruby-forum.com/t/how-to-add-an-after-save-callback-on-runtime-on-a-particular-instance/188445 "2010-05-12T04:17:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nadal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/6f9a4e/32.png) [@Nadal](https://www.ruby-forum.com/u/Nadal)\
**Post date:** [May 12, 2010, 4:17am UTC](https://www.ruby-forum.com/t/how-to-add-an-after-save-callback-on-runtime-on-a-particular-instance/188445/1 "2010-05-12T04:17:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Nadal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/6f9a4e/32.png) [@Nadal](https://www.ruby-forum.com/u/Nadal)\
**Post date:** [May 12, 2010, 10:25am UTC](https://www.ruby-forum.com/t/how-to-add-an-after-save-callback-on-runtime-on-a-particular-instance/188445/2 "2010-05-12T10:25:46Z")

</div>

On 12 May 2010 03:15, Nadal [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![Nadal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/6f9a4e/32.png) [@Nadal](https://www.ruby-forum.com/u/Nadal)\
**Post date:** [May 12, 2010, 10:53am UTC](https://www.ruby-forum.com/t/how-to-add-an-after-save-callback-on-runtime-on-a-particular-instance/188445/3 "2010-05-12T10:53:16Z")

</div>

On May 12, 3:15 am, Nadal [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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
