RailsCasts Pro episodes are now free!
Learn more or hide this
GitHub User: dsdewhir
@Minh-Tue Every time you go to the server you get a session, and every session can have any arbitrary amount of objects stored on the server as session variables. Further, the server is smart enough to give every user their OWN session with a distinct ID. When you see things like 'session[:user_id] = user.id' it is just telling the server, essentially, "Create a new session variable named user_id and assign it the value of user.id." You can do this for anything you want, naming your variable anything you want -- for example, if your user table had a firstname column you could do session[:cool_name] = user.firstname and then use session[:cool_name] wherever you wanted to display the current user's firstname.
As far as multiple users go, the server writes a cookie to your browser with your unique session ID in it, which is how it keeps track of which session variable values to return to you so that you see your firstname, and not the value of session[:cool_name] for some other user.