Session magic question

Greetings!

I could really use some help understanding what Rails puts into the
session store related to the objects it creates. I’ve found
documentation on how I can put / get info into it, but can’t find the
docs on what Rails puts into it.

I’ve got a simple app that collects some data from the user, stores it
in a db record, allows the user to edit it, display it, and save it to
an XML file. Right now it’s real simple: 1 table.

I want the user to be able to create one and only one record in that
table during a session. There’s a menu item that lets the user enter
and / or edit the data in that record. The first time they select that
item they get a blank form, fill it in, and save it. If they select
that item again, the request gets redirected to the ‘edit’ action and
they get a form with the existing data displayed. I figure to check the
session to see if there’s an entry for that object (like the Cart object
in the Depot app) to drive the logic. I’m assuming this will work,
but…

Can someone point me to some documentation on just exactly what Rails is
putting into the session store, how to access it, etc?

Thanks,
Bill

Hello,

Rails don’t put anything in session relative to database access.

You have to deal with your oneshoot insert in the database.

That’s quickly what I’ve in mind regarding to your needs. You can also
look near before_insert filter, and other filters or validation.

def save
@session[:tracker] ||= 1 # initialize that session key at 1 if not
allready defined
if @session[:tracker]==1
don’t save, redirect to home
end
really save
end

Cheers,

Hi Mathieu,

Thanks for the quick reply. I’m a little confused, I think. I’ve got a
model Class named ‘emrec’. It’s an AR class based on the ‘emrecs’ table
so,
yeah, it’s got to do with the database. But I thought that when I
created
@emrec = Emrec.new’ that Rails put an entry for that object into the
session hash. I thought, further, that I could find out whether or not
the
object already existed by checking to see if there was an entry for it
in
the session hash (e.g., @emrec = session[:emrec] ||= Emrec.new). No?

Thanks,
Bill

----- Original Message -----
From: “Mathieu C.” [email protected]
To: [email protected]
Sent: 2006-02-28 8:53 AM
Subject: Re: [Rails] Session magic question

Hello,

Rails don’t put anything in session relative to database access.

You have to deal with your oneshoot insert in the database.

That’s quickly what I’ve in mind regarding to your needs. You can also
look near before_insert filter, and other filters or validation.

def save
@session[:tracker] ||= 1 # initialize that session key at 1 if not
allready defined
if @session[:tracker]==1
don’t save, redirect to home
end
really save
end

Cheers,


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Hi Ben,

Thanks for the reply. If what you say is true, then I really don’t
understand how the shopping cart functionality in the Depot application
works. On p.81 of AWD the implementation of the find_cart method is:

def find_cart
session[:cart] ||= Cart.new
end

It’s explained as follows…

“If the session hash has a value corresponding to the key ‘:cart’, that
value is returned immediately. Otherwise a new cart object is created
and
assigned to the session. This new cart is then returned.”

I assumed ‘Cart.new’ did the assignment to the session. If not, how’s
it
done? There’s no place anywhere in the app that I can find that does an
explicit assignment of the Cart object id to the session. Help, please
?!?!?!? :wink:

Thanks,
Bill

----- Original Message -----
From: “Ben M.” [email protected]
To: [email protected]
Sent: 2006-02-28 11:01 AM
Subject: Re: [Rails] Session magic question

No… I don’t think anything gets put in the session unless you put it
there (and you have
to remove it too).

b

Bill W. wrote:

Hi Mathieu,

Thanks for the quick reply. I’m a little confused, I think. I’ve got a
model Class named ‘emrec’. It’s an AR class based on the ‘emrecs’ table
so,
yeah, it’s got to do with the database. But I thought that when I
created
@emrec = Emrec.new’ that Rails put an entry for that object into the
session hash. I thought, further, that I could find out whether or not
the
object already existed by checking to see if there was an entry for it
in

No… I don’t think anything gets put in the session unless you put it
there (and you have
to remove it too).

b

On Feb 28, 2006, at 11:47 AM, Bill W. wrote:

session[:cart] ||= Cart.new

I assumed ‘Cart.new’ did the assignment to the session. If not,
how’s it
done? There’s no place anywhere in the app that I can find that
does an
explicit assignment of the Cart object id to the session. Help,
please

session[:cart] ||= Cart.new

is the same as

session[:cart] = session[:cart] || Cart.new

There’s an assignment in there :slight_smile:

Dave

Hi Brian,

Brian V. Hughes wrote:

I’m not Ben, but I can answer this one fairly easily…

The piece that you are missing with your example from AWD is the fact
that the Cart model object isn’t an Active Record object. It’s simply a
class defined within app/models, used to hold a hash object that
contains the shopping cart items. A brand new Cart object is being
stored in the session (if it doesn’t already exist) in the find_cart
method.

Are you saying that, if the Cart object was not an AR object, that it
(or
it’s ID I guess) would not be stored in the session?

Sorry I’m being so dense on this. Thanks for your help!

Best regards,
Bill

Hi Dave,

Dave T. wrote:

session[:cart] ||= Cart.new

is the same as

session[:cart] = session[:cart] || Cart.new

There’s an assignment in there :slight_smile:

So the Cart.new method does put the Cart id value in the session
store?
Does ‘.new’ do that for all new objects? What does it do for the
second,
third, etc. instance of that Class? Do I end up with something like
@carts
= session[:cart.each]? I hate to keep asking these nuby questions and
will
be very happy to go away and study the documentation if you’ll point me
to
it.

Thanks!
Bill

So the Cart.new method does put the Cart id value in the session store?

No Bill, you are wrong here, it is the “=” in: “session[:cart] =
Cart.new”
who stores that new cart instance in the session.

----- Original Message -----

I’m not Ben, but I can answer this one fairly easily…

The piece that you are missing with your example from AWD is the fact
that the Cart model object isn’t an Active Record object. It’s simply a
class defined within app/models, used to hold a hash object that
contains the shopping cart items. A brand new Cart object is being
stored in the session (if it doesn’t already exist) in the find_cart
method.

-Brian

Bill W. wrote:

Are you saying that, if the Cart object was not an AR object, that it (or
it’s ID I guess) would not be stored in the session?

I believe the answer is yes, but you’ve got several extra negatives in
there. :wink:

To be more clear, I’m not sure you’d want to store an entire AR object
in the session hash. If you need to carry around easy access to an AR
object, request to request, then I would simply store the ID:
session[:foo_id] = foo.id (where foo is an AR object.

Sorry I’m being so dense on this. Thanks for your help!

Not a problem. This stuff can be a little confusing.

-Brian

On Feb 28, 2006, at 12:35 PM, Bill W. wrote:

Maybe I’m confused on local vs. instance variables?
The wiki’s out of date. Both work, but session[] is preferred over
@session.

Dave

On Feb 28, 2006, at 10:35 AM, Bill W. wrote:

Ah (he says wishing he hadn’t responded so quickly earlier ;-p )

So Cart.new returns the object id which gets passed through the
assignment
operator to session[:cart].

Cart.new doesn’t return the object id, it returns the object itself.


– Tom M.

Hi again,

Dave T. wrote:

session[:cart] ||= Cart.new

is the same as

session[:cart] = session[:cart] || Cart.new

There’s an assignment in there :slight_smile:

Ah (he says wishing he hadn’t responded so quickly earlier ;-p )

So Cart.new returns the object id which gets passed through the
assignment
operator to session[:cart].

Maybe what’s throwing me it that the documentation on the Rails wiki
(Peak Obsession) shows
assignments being done to what I thought was an instance variable:

@session[:greeting] = “Hello world!”

How come the Depot app doesn’t need to use @session[] instead of
session[]
to store the Cart id?

Maybe I’m confused on local vs. instance variables?

Thanks,
Bill

On 2/28/06, Bill W. [email protected] wrote:

to store the Cart id?

Maybe I’m confused on local vs. instance variables?

Based on your questions, and that you really seem to want to learn
this, I’d recommend Ruby for Rails. It’s in beta right now, but the
available PDF chapters already cover a lot of what you’re asking about
here.

How come the Depot app doesn’t need to use @session[] instead of
session[]
to store the Cart id?

Maybe I’m confused on local vs. instance variables?

No, you’re confused because there’s two ways to do it.

And, it’s an instance variable (@session) -vs- a method call (session
[]) in
this case, I believe.


– Tom M.

At 2/28/2006 01:53 PM, you wrote:

to store the Cart id?

Maybe I’m confused on local vs. instance variables?

The wiki’s out of date. Both work, but session[] is preferred over
@session.

Dave

I took that as a request to update the wiki :wink: (I’d received the
same answer from Scott B. a few weeks ago.)

-Rob

Hi James,

Thanks for the recommendation. I haven’t bought it yet, but intend to.

Best regards,
Bill

----- Original Message -----
From: “James L.” [email protected]
To: [email protected]
Sent: 2006-02-28 2:32 PM
Subject: Re: [Rails] Session magic question

On 2/28/06, Bill W. [email protected] wrote:

to store the Cart id?

Maybe I’m confused on local vs. instance variables?

Based on your questions, and that you really seem to want to learn
this, I’d recommend Ruby for Rails. It’s in beta right now, but the
available PDF chapters already cover a lot of what you’re asking about
here.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

On Feb 28, 2006, at 1:04 PM, Bill W. wrote:

The root of the problem (which I obscured with a couple of others)

Isn’t learning wonderful? :slight_smile:

It’s not a job, it’s an adventure!


– Tom M.

Hi Tom, and everyone who’s contributed to helping me get this!

It turns out I was wrong on all fronts…

I thought I was having a problem with the session hash because the
record
wasn’t getting updated. Turns out it wasn’t the session at all. The
root
of the problem (which I obscured with a couple of others) turns out to
be
that I wasn’t putting the data in the object using ‘update_attributes’.
Duh
:stuck_out_tongue:

Thanks again for your help!

Bill

----- Original Message -----
From: “Tom M.” [email protected]
To: [email protected]
Sent: 2006-02-28 2:37 PM
Subject: Re: [Rails] Session magic question