# Session is nil inside model

**URL:** <https://www.ruby-forum.com/t/session-is-nil-inside-model/90018>\
**Category:** Rails\
**Created:** [March 25, 2007, 3:09am UTC](https://www.ruby-forum.com/t/session-is-nil-inside-model/90018 "2007-03-25T03:09:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stubbsuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e56c9b/32.png) [@stubbsuk](https://www.ruby-forum.com/u/stubbsuk)\
**Post date:** [March 25, 2007, 3:09am UTC](https://www.ruby-forum.com/t/session-is-nil-inside-model/90018/1 "2007-03-25T03:09:14Z")

</div>

I’m tring to write a little function to indicate if a user is able to  
pay an invoice, I have my invoice model with:

def canPay?  
if session[:user].id == self.consumer\_id  
return true;  
else  
return false;  
end  
end

as a method, but it gives the error:

“undefined local variable or method `session’ for #\<Invoice:  
0xb7670928\>”

Right before the method is called, I put a breakpoint, and session is  
not nil, it has the user object in it as I expected it to.

---

<div class="post-metadata">

**Author:** ![stubbsuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e56c9b/32.png) [@stubbsuk](https://www.ruby-forum.com/u/stubbsuk)\
**Post date:** [March 25, 2007, 4:28am UTC](https://www.ruby-forum.com/t/session-is-nil-inside-model/90018/2 "2007-03-25T04:28:18Z")

</div>

Stuart G. wrote:

> as a method, but it gives the error:
> 
> “undefined local variable or method `session’ for #\<Invoice:  
> 0xb7670928\>”
> 
> Right before the method is called, I put a breakpoint, and session is  
> not nil, it has the user object in it as I expected it to.

The session object is local to the controller (and the view rendered by  
the controller). The simple workaround is to pass in session[:user] as a  
parameter to canPay?

–  
Michael W.

---

<div class="post-metadata">

**Author:** ![stubbsuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e56c9b/32.png) [@stubbsuk](https://www.ruby-forum.com/u/stubbsuk)\
**Post date:** [March 25, 2007, 10:53am UTC](https://www.ruby-forum.com/t/session-is-nil-inside-model/90018/3 "2007-03-25T10:53:51Z")

</div>

Michael W. wrote:

> Stuart G. wrote:
> 
> > I’m tring to write a little function to indicate if a user is able to  
> > pay an invoice, I have my invoice model with:
> 
> The session object is local to the controller (and the view rendered by  
> the controller). The simple workaround is to pass in session[:user] as a  
> parameter to canPay?

And the reason why the session is not available is that doing so would  
break one of the core principles of the Model View Controller pattern:  
the model should be completely unaware of the presentation layer.

You never want the model to use a session or an http request or generate  
html. The model should contain your business data and the logic to  
manipulate it.

Anyway, sorry to be pedantic, but that’s why the session ain’t  
available… just pass the data you need to the method (or maybe some  
earlier work that the controller does with the invoice model puts it  
into a state where it is ready to answer the can\_pay? message).

b

---

<div class="post-metadata">

**Author:** ![stubbsuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e56c9b/32.png) [@stubbsuk](https://www.ruby-forum.com/u/stubbsuk)\
**Post date:** [March 25, 2007, 1:50pm UTC](https://www.ruby-forum.com/t/session-is-nil-inside-model/90018/4 "2007-03-25T13:50:05Z")

</div>

On Mar 25, 9:52 am, Ben M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Anyway, sorry to be pedantic, but that’s why the session ain’t  
> available… just pass the data you need to the method (or maybe some  
> earlier work that the controller does with the invoice model puts it  
> into a state where it is ready to answer the can\_pay? message).

Thanks Michael & Ben, makes perfect sense.
