How to save an object with its associations into session

Hi,

How do I save an object with its children into the session? By default
AR cleans the children when I put the object there.

And if someone knows any docs about that please show the link.

Thks,

Bruno R.

What do you mean it cleans by default ?

Start
session[:user] = User.find(1)

then you can do this
session[:user].id

or that?
session[:user].contacts.length

that is what I can’t do: session[:user].contacts.length

On Oct 11, 11:11 am, Jamal S. [email protected]

Bruno R. wrote:

How do I save an object with its children into the session? By default
AR cleans the children when I put the object there.

And if someone knows any docs about that please show the link.

In general, it’s a bad idea to put a full AR object in the session. It
mostly works, but it can get you in trouble. The accepted best practice
is to store the id in the session, then fetch the record from the
database when you need it. That way you avoid the nasty synchronization
issues etc.

session[:user_id] = user.id

then later…

user = User.find(session[:user.id])


Josh S.
http://blog.hasmanythrough.com

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs