I'm a little confused about the convenience of using setters and getters inside a Rails 3 application controller or helper. Why does someone use setters and getters in a controller method (or module), and not just in an instance variable. Can someone give an example? Can I use setters and getters? And when is it necessary?
For example, in the Rubby on Rails 3 tutorial by Michael Hartl (p. 347):
SessionsHelper Module
def current_user=(user) @current_user = user end def current_user @current_user ||= user_from_remember_token end
Why not just use @current_user in the first place.
My second question is what does the value of self inside the controller method mean. For instance:
Class SessionsController < ApplicationController def sign_in? cookies.permanent.signed[:remember_token] = [user.id, user.salt] self.current_user= user end end
I know that the inside of the user model class itself refers to the user itself. But when it is inside the controller, what does this apply to? Any example?
thanks
source share