# User data in session or instance variable?

**URL:** <https://www.ruby-forum.com/t/user-data-in-session-or-instance-variable/209393>\
**Category:** Rails\
**Created:** [July 13, 2011, 9:37am UTC](https://www.ruby-forum.com/t/user-data-in-session-or-instance-variable/209393 "2011-07-13T09:37:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [July 13, 2011, 9:37am UTC](https://www.ruby-forum.com/t/user-data-in-session-or-instance-variable/209393/1 "2011-07-13T09:37:06Z")

</div>

Firstly I’m coming from a Java EE background and building up my Rails  
knowledge slowly in my (relatively limited!) free time, so apologies  
if this is rudimentary stuff.

The common pattern for user sessions seems to be something like  
‘session[:id] = user.id’ in the controller and  
‘User.find(session[:id])’ in the view.

However its also possible to just set an instance variable once a user  
has logged in, e.g. ‘@id = user.id’. Due to some rails magic the  
instance variable of the controller gets passed to the view, thus  
facilitating a kind of session persistence. I’d hazard a guess at  
saying this is probably bad form but due to not fully understanding  
how the instance variable is available I’m not sure.

What’s the general consensus on this?

---

<div class="post-metadata">

**Author:** ![ses](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8baadc/32.png) [@ses](https://www.ruby-forum.com/u/ses)\
**Post date:** [July 13, 2011, 9:43am UTC](https://www.ruby-forum.com/t/user-data-in-session-or-instance-variable/209393/2 "2011-07-13T09:43:16Z")

</div>

On 12 Jul 2011, at 22:02, ses [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> instance variable of the controller gets passed to the view, thus  
> facilitating a kind of session persistence. I’d hazard a guess at  
> saying this is probably bad form but due to not fully understanding  
> how the instance variable is available I’m not sure.

The passing of instance variables from controller to view is fairly  
central to rails. If anything, it is doing too much in the view that is  
frowned upon. Personally I would do something like

@user = User.find session[:id] in the controller

And then use @user in the view

Fred

---

<div class="post-metadata">

**Author:** ![ses](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8baadc/32.png) [@ses](https://www.ruby-forum.com/u/ses)\
**Post date:** [July 13, 2011, 11:15am UTC](https://www.ruby-forum.com/t/user-data-in-session-or-instance-variable/209393/3 "2011-07-13T11:15:23Z")

</div>

Definately @user = User.find(session[:id]) would go in the controller.

You can also make a helper called something like current\_user so you  
can access the user in the view without having it to set in the  
controller action every time.  
This makes it possible to write something like:

\<%= current\_user.name %\> in the view.

Google stuff on Rails helpers but I normally try to not get them  
bloated. Its the old saying: Fat model, skinny controller.  
As Fred pointed out its frowned upon to have logic in the view.

Regards  
Stefano

On Jul 13, 9:41am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ses](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8baadc/32.png) [@ses](https://www.ruby-forum.com/u/ses)\
**Post date:** [July 14, 2011, 4:53pm UTC](https://www.ruby-forum.com/t/user-data-in-session-or-instance-variable/209393/4 "2011-07-14T16:53:22Z")

</div>

On Jul 13, 4:18pm, Norm S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > if this is rudimentary stuff.  
> > how the instance variable is available I’m not sure.  
> > Norm  
> > Thanks for all your responses, yes I realise now that this is the  
> > case. Rails documentation seems somewhat lacking but then stuff is  
> > just so much simpler and more logical so on the whole its not a  
> > problem.

---

<div class="post-metadata">

**Author:** ![ses](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8baadc/32.png) [@ses](https://www.ruby-forum.com/u/ses)\
**Post date:** [July 13, 2011, 5:19pm UTC](https://www.ruby-forum.com/t/user-data-in-session-or-instance-variable/209393/5 "2011-07-13T17:19:31Z")

</div>

On 07/12/2011 02:02 PM, ses wrote:

> instance variable of the controller gets passed to the view, thus  
> facilitating a kind of session persistence. I’d hazard a guess at  
> saying this is probably bad form but due to not fully understanding  
> how the instance variable is available I’m not sure.
> 
> What’s the general consensus on this?

The real experts have already weighed in on this but I thought I would  
make this one comment.  
ROR is in most regards stateless so while an instance variable like  
@user is fine to communicate from controller to view you will have to  
use the session variables to communicate from one controller invocation  
to the next.

Norm
