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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060228/b7b3e77b/attachment.html
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 Chappuis" <mathieu.chappuis.lists@gmail.com> To: <rails@lists.rubyonrails.org> 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 Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
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 Walton 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 > the session hash (e.g., @emrec = session[:emrec] ||= Emrec.new). No? > > Thanks, > Bill > > ----- Original Message ----- > From: "Mathieu Chappuis" <mathieu.chappuis.lists@gmail.com> > To: <rails@lists.rubyonrails.org> > 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 > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > 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 ?!?!?!? ;-) Thanks, Bill ----- Original Message ----- From: "Ben Munat" <bent@munat.com> To: <rails@lists.rubyonrails.org> 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 itthere (and you have> to remove it too). > > b > > Bill Walton 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'' tableso,> > yeah, it''s got to do with the database. But I thought that when Icreated> > ''@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 notthe> > object already existed by checking to see if there was an entry for itin> > the session hash (e.g., @emrec = session[:emrec] ||= Emrec.new). No? > > > > Thanks, > > Bill > > > > ----- Original Message ----- > > From: "Mathieu Chappuis" <mathieu.chappuis.lists@gmail.com> > > To: <rails@lists.rubyonrails.org> > > 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 > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On Feb 28, 2006, at 11:47 AM, Bill Walton 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, > pleasesession[:cart] ||= Cart.new is the same as session[:cart] = session[:cart] || Cart.new There''s an assignment in there :) Dave
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 Walton wrote:> 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
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_cartmethod. 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 Thomas 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
> 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 ----- "Bill Walton wrote:> Hi Dave, > > Dave Thomas 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
Bill Walton 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
Hi again, Dave Thomas 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/HowtoWorkWithSessions) 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 Feb 28, 2006, at 10:35 AM, Bill Walton wrote:> Dave Thomas 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].Cart.new doesn''t return the object id, it returns the object itself. -- -- Tom Mornini
On Feb 28, 2006, at 12:35 PM, Bill Walton wrote:> Maybe what''s throwing me it that the documentation on the Rails wiki > (http://wiki.rubyonrails.com/rails/pages/HowtoWorkWithSessions) 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?The wiki''s out of date. Both work, but session[] is preferred over @session. Dave
On 2/28/06, Bill Walton <bill.walton@charter.net> wrote:> 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/HowtoWorkWithSessions) 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?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
> On 2/28/06, Bill Walton <bill.walton@charter.net> wrote: >> 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/HowtoWorkWithSessions) 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?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 Mornini
At 2/28/2006 01:53 PM, you wrote:>On Feb 28, 2006, at 12:35 PM, Bill Walton wrote: > >>Maybe what''s throwing me it that the documentation on the Rails wiki >>(http://wiki.rubyonrails.com/rails/pages/HowtoWorkWithSessions) 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? > > >The wiki''s out of date. Both work, but session[] is preferred over >@session. > >DaveI took that as a request to update the wiki ;-) (I''d received the same answer from Scott Barron 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 Ludlow" <jamesludlow@gmail.com> To: <rails@lists.rubyonrails.org> Sent: 2006-02-28 2:32 PM Subject: Re: [Rails] Session magic question On 2/28/06, Bill Walton <bill.walton@charter.net> wrote:> 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/HowtoWorkWithSessions) 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?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 Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
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 Mornini" <tmornini@infomania.com> To: <rails@lists.rubyonrails.org> Sent: 2006-02-28 2:37 PM Subject: Re: [Rails] Session magic question> > On 2/28/06, Bill Walton <bill.walton@charter.net> wrote: > >> 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/HowtoWorkWithSessions) 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? > > 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 Mornini > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails