Forum: Ruby on Rails Session magic question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bill W. (Guest)
on 2006-02-28 16:40
(Received via mailing list)
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
Mathieu C. (Guest)
on 2006-02-28 16:55
(Received via mailing list)
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,
Bill W. (Guest)
on 2006-02-28 17:39
(Received via mailing list)
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." <removed_email_address@domain.invalid>
To: <removed_email_address@domain.invalid>
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
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails
Ben M. (Guest)
on 2006-02-28 18:59
(Received via mailing list)
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. (Guest)
on 2006-02-28 20:04
(Received via mailing list)
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
?!?!?!? ;-)

Thanks,
Bill


----- Original Message -----
From: "Ben M." <removed_email_address@domain.invalid>
To: <removed_email_address@domain.invalid>
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
Dave T. (Guest)
on 2006-02-28 20:07
(Received via mailing list)
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 :)


Dave
Brian V. Hughes (Guest)
on 2006-02-28 20:10
(Received via mailing list)
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. (Guest)
on 2006-02-28 20:25
(Received via mailing list)
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
Bill W. (Guest)
on 2006-02-28 20:34
(Received via mailing list)
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 :)

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
Juanjo B. (Guest)
on 2006-02-28 20:43
(Received via mailing list)
> 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 -----
Brian V. Hughes (Guest)
on 2006-02-28 20:43
(Received via mailing list)
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. ;)

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
Bill W. (Guest)
on 2006-02-28 20:49
(Received via mailing list)
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 :)

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
(http://wiki.rubyonrails.com/rails/pages/HowtoWorkW...) 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
Dave T. (Guest)
on 2006-02-28 20:56
(Received via mailing list)
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
Tom M. (Guest)
on 2006-02-28 20:56
(Received via mailing list)
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.
James L. (Guest)
on 2006-02-28 22:33
(Received via mailing list)
On 2/28/06, Bill W. <removed_email_address@domain.invalid> 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.

http://www.manning.com/books/black
Tom M. (Guest)
on 2006-02-28 22:38
(Received via mailing list)
>>
>> 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.
Rob B. (Guest)
on 2006-02-28 22:47
(Received via mailing list)
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 ;-)  (I'd received the
same answer from Scott B. a few weeks ago.)

-Rob
Bill W. (Guest)
on 2006-02-28 23:10
(Received via mailing list)
Hi James,

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

Best regards,
Bill

----- Original Message -----
From: "James L." <removed_email_address@domain.invalid>
To: <removed_email_address@domain.invalid>
Sent: 2006-02-28 2:32 PM
Subject: Re: [Rails] Session magic question


On 2/28/06, Bill W. <removed_email_address@domain.invalid> 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.

http://www.manning.com/books/black
_______________________________________________
Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails
Bill W. (Guest)
on 2006-02-28 23:18
(Received via mailing list)
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
:-P

Thanks again for your help!

Bill


----- Original Message -----
From: "Tom M." <removed_email_address@domain.invalid>
To: <removed_email_address@domain.invalid>
Sent: 2006-02-28 2:37 PM
Subject: Re: [Rails] Session magic question
Tom M. (Guest)
on 2006-02-28 23:22
(Received via mailing list)
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? :-)

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

--
-- Tom M.
This topic is locked and can not be replied to.