# Call\_backs Is it?

**URL:** <https://www.ruby-forum.com/t/call-backs-is-it/140574>\
**Category:** Rails\
**Created:** [June 16, 2008, 12:13pm UTC](https://www.ruby-forum.com/t/call-backs-is-it/140574 "2008-06-16T12:13:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [June 16, 2008, 12:13pm UTC](https://www.ruby-forum.com/t/call-backs-is-it/140574/1 "2008-06-16T12:13:23Z")

</div>

Hi  
I have the models  
1.ServiceDeskTicket with fields

id — number — service\_desk\_status\_id – created\_on – updated\_on  
etc

2.ServiceDeskActivity with fields

id – service\_desk\_ticket\_id – description – created\_on –  
updated\_on

3.ServiceDeskAttachment  
id – service\_desk\_ticket\_id – attachment – created\_on –  
updated\_on

```
 Relations as

```

ServiceDeskticket has\_many service\_desk\_activities  
ServiceDeskticket has\_many service\_desk\_attachments  
ServiceDeskActivity belongs service\_desk\_ticket  
ServiceDeskAttachment belongs\_to service\_desk\_ticket

```
   Now my requiremnet is whenever a new service desk activity as

```

well as service desk attachment is added the updated\_on field of  
ServiceDeskTicket is to be updated…(created\_on and updated\_on magic  
fields of rails)…Can I set this in ServiceDeskTicket class like a  
call\_back…Since I am very new to rails call back functionality asking  
like this…Please correct if wrong…How can I do this without rewritng  
the code(I ma not asking update\_attribute)…If I continue like  
sd\_ticket.update\_attribute(‘fieldname’,value) i have to do in all the  
definitions .How can I avoid this

Thanks in advance  
Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [June 16, 2008, 1:18pm UTC](https://www.ruby-forum.com/t/call-backs-is-it/140574/2 "2008-06-16T13:18:20Z")

</div>

Sijo Kg wrote:

> ```
> Now my requiremnet is whenever a new service desk activity as
> 
> ```
> 
> well as service desk attachment is added the updated\_on field of  
> ServiceDeskTicket is to be updated…(created\_on and updated\_on magic  
> fields of rails)…Can I set this in ServiceDeskTicket class like a  
> call\_back…Since I am very new to rails call back functionality asking  
> like this…Please correct if wrong…How can I do this without rewritng  
> the code(I ma not asking update\_attribute)…If I continue like  
> sd\_ticket.update\_attribute(‘fieldname’,value) i have to do in all the  
> definitions .How can I avoid this
> 
> Thanks in advance  
> Sijo

Your callbacks should go in the class that triggers the actions when it  
is saved. eg

#in service\_desk\_activity.rb  
before\_save :update\_ticket

def update\_ticket  
self.service\_desk\_ticket.save  
end

because you’re using the magic timestamps, just saving the associated  
ticket object should update updated\_at correctly. You can use the same  
code in the ‘attachment’ class as well i think.

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [June 16, 2008, 1:30pm UTC](https://www.ruby-forum.com/t/call-backs-is-it/140574/3 "2008-06-16T13:30:55Z")

</div>

Hi  
Thanks for your reply.  
Sijo
