views:

202

answers:

1

With Rails 3, the default session storage mechanism is cookie_store. I assume that this means that the contents within the session hash are serialized, encoded and stored within a cookie in the browser? Does this mean that nothing (or very little) of the session is stored in the server?

I've had a few issues where I had a cookie overflow error and I'm assuming because I kept on adding to my user instance (which was also linked/fetched from the cookie).

u = session[:user]
u.add_this lots_of_data

so eventually I got a cookie overflow error.

Am I correct about this? Are sessions fully stored within cookies in Rails 3 (by default)?

+3  A: 

Yes, if you use the cookie store, the session data is stored in the cookie. If you'd like to store it on the server, you will need to use another session store.

However, if you are storing model objects or "lots of data" in the session, you are most likely doing it wrong in the first place. Your data should go to the database, and the session should only contain as much information as you need to retrieve it.

In you case, this would mean to store the user id int he session, and load the user from the db in a before_filter.

averell
Yes, exactly. In other words, don't store whole models in the session when you can just store their database ID.
Andrew Vit
Right. Exactly! The question remains is that do the models themselves populate and get bigger and bigger within the cookie as you populate them on the server?
matsko
If you put the id in the session, you will have the id in the session, nothing else. If put a full object in the session (as in your example above), it will take as much space as it needs to serialize the object...
averell