# Session In the model

**URL:** <https://www.ruby-forum.com/t/session-in-the-model/155548>\
**Category:** Rails\
**Created:** [December 13, 2008, 7:04am UTC](https://www.ruby-forum.com/t/session-in-the-model/155548 "2008-12-13T07:04:39Z")\
**Posts on this page:** 4\
**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:** [December 13, 2008, 7:04am UTC](https://www.ruby-forum.com/t/session-in-the-model/155548/1 "2008-12-13T07:04:39Z")

</div>

Hi  
i have the model SDTicket which has a field modified\_by\_id and it has  
to take data from session[:id] Now this SDTicket model has many  
associated models like Activity one of them So when ever an activity  
happens then also the value in session[:id] is to be go to  
modified\_by\_id field

SDTicket has\_many activities  
Activity belongs to SDTicket

```
 So for that what I am trying is in Activity model wrote in

```

after\_save like below  
after\_save :update\_sd\_ticket  
def update\_sd\_ticket  
#Here I dont know how to get that session[:id] and fill that to  
modified\_by\_id  
self.service\_desk\_ticket.save  
end

```
   I also tried like to get session from a module by including it in

```

activity model But that too not working.Could you please help to solve  
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:** [December 18, 2008, 1:55am UTC](https://www.ruby-forum.com/t/session-in-the-model/155548/2 "2008-12-18T01:55:10Z")

</div>

Sijo Kg wrote:

> ```
> So for that what I am trying is in Activity model wrote in
> 
> ```
> 
> this  
> The session hash is only available in the controller and view.

You’ll have to add a line to your controller method before your  
call to save

activity.modified\_by\_id = session[:id]

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)

---

<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:** [December 18, 2008, 2:53pm UTC](https://www.ruby-forum.com/t/session-in-the-model/155548/3 "2008-12-18T14:53:58Z")

</div>

This blog post outlines a common way to get the current logged in user  
in your models…

> **[Making Session Data Available to Models in Ruby on Rails](https://www.zorched.net/2007/05/29/making-session-data-available-to-models-in-ruby-on-rails/)**

---

<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:** [December 19, 2008, 10:11am UTC](https://www.ruby-forum.com/t/session-in-the-model/155548/4 "2008-12-19T10:11:50Z")

</div>

Hi  
Thanks for the link  
Sijo
