Bakki Kudva
2006-May-26 18:22 UTC
[Rails] shouldn''t this work? - session[:array_of_objects].delete_if {|x| x.id == params[:id]}
I am saving an array of active record objects (college courses) in the session as session[:course_list]. This is the cart for a registration controller. If a student chooses to delete the course from the list I get the :id as params[:id]. However if I do session[:course_list].delete_if {|x| x.id ==params[:id]} does not work. session[:course_list].delete_at(idx) works. Ocourse to do this I have to index the list in the view and insert the value into each link_to. When I try this in irb the second statement works but the first does not and does not give any error messages. What am I missing here? I have stared at this long enough and can''t figure it out. TIA, bakki
Daniel Higginbotham
2006-May-26 18:49 UTC
[Rails] shouldn''t this work? - session[:array_of_objects].delete_if{|x| x.id == params[:id]}
The only thing I can think of is that params[:id] isn''t actually being set? -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Bakki Kudva Sent: Friday, May 26, 2006 8:22 AM To: rails list Subject: [Rails] shouldn''t this work? - session[:array_of_objects].delete_if{|x| x.id == params[:id]} I am saving an array of active record objects (college courses) in the session as session[:course_list]. This is the cart for a registration controller. If a student chooses to delete the course from the list I get the :id as params[:id]. However if I do session[:course_list].delete_if {|x| x.id ==params[:id]} does not work. session[:course_list].delete_at(idx) works. Ocourse to do this I have to index the list in the view and insert the value into each link_to. When I try this in irb the second statement works but the first does not and does not give any error messages. What am I missing here? I have stared at this long enough and can''t figure it out. TIA, bakki _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
Bakki Kudva
2006-May-26 19:14 UTC
[Rails] shouldn''t this work? - session[:array_of_objects].delete_if{|x| x.id == params[:id]}
It is. I checked in irb using breakpointer. Also when I directly execute the code in irb with manually entered value nothing happens. eg. session[:array_of_objects].delete_if {|x| x.id == "47"} I am going to try this again with all stale sessions removed. Thanks, bakki On 5/26/06, Daniel Higginbotham <daniel@flyingmachinestudios.com> wrote:> The only thing I can think of is that params[:id] isn''t actually being set? > > -----Original Message----- > From: rails-bounces@lists.rubyonrails.org > [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Bakki Kudva > Sent: Friday, May 26, 2006 8:22 AM > To: rails list > Subject: [Rails] shouldn''t this work? - > session[:array_of_objects].delete_if{|x| x.id == params[:id]} > > I am saving an array of active record objects (college courses) in the > session as session[:course_list]. This is the cart for a registration > controller. > > If a student chooses to delete the course from the list I get the :id > as params[:id]. > > However if I do > session[:course_list].delete_if {|x| x.id ==params[:id]} > does not work. > > session[:course_list].delete_at(idx) > works. Ocourse to do this I have to index the list in the view and > insert the value into each link_to. > > When I try this in irb the second statement works but the first does > not and does not give any error messages. > > What am I missing here? I have stared at this long enough and can''t > figure it out. > > TIA, > > bakki > _______________________________________________ > 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 >
Brian Hughes
2006-May-26 19:24 UTC
[Rails] shouldn''t this work? - session[:array_of_objects].delete_if{|x| x.id == params[:id]}
On May 26, 2006, at 03:04 PM, Bakki Kudva wrote:> It is. I checked in irb using breakpointer. Also when I directly > execute the code in irb with manually entered value nothing happens. > eg. > session[:array_of_objects].delete_if {|x| x.id == "47"}There''s your problem... If you are actually storing Model objects in that session array, then the id attribute will be an integer. You can''t directly compare integers to strings and hope to get a match. Notice: irb(main):001:0> 47 == "47" => false irb(main):002:0> 47 == "47".to_i => true Modify your delete_if statement to coerce params[:id] to an integer and you should be all set. -Brian
Bakki Kudva
2006-May-27 18:01 UTC
[Rails] shouldn''t this work? - session[:array_of_objects].delete_if{|x| x.id == params[:id]}
Brian, Thank you very much for your response. I did figure it out yesterday after mucking around in irb for a while and actutally looked at the parms hash. Thanks again, bakki On 5/26/06, Brian Hughes <brianvh@alum.dartmouth.org> wrote:> On May 26, 2006, at 03:04 PM, Bakki Kudva wrote: > > It is. I checked in irb using breakpointer. Also when I directly > > execute the code in irb with manually entered value nothing happens. > > eg. > > session[:array_of_objects].delete_if {|x| x.id == "47"} > > There''s your problem... If you are actually storing Model objects in > that session array, then the id attribute will be an integer. You > can''t directly compare integers to strings and hope to get a match. > Notice: > > irb(main):001:0> 47 == "47" > => false > irb(main):002:0> 47 == "47".to_i > => true > > Modify your delete_if statement to coerce params[:id] to an integer > and you should be all set. > > -Brian > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >