Session is nil inside model

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.

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.

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

On Mar 25, 9:52 am, Ben M. [email protected] 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.