Forum: Ruby on Rails shouldn't this work? - session[:array_of_objects].delete_if

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.
8acba6d057b3f0c2c16cca95ad36881b?d=identicon&s=25 Bakki Kudva (bakki)
on 2006-05-26 20:22
(Received via mailing list)
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
43ec5998360a0cc21b51afef1781e9c0?d=identicon&s=25 Daniel Higginbotham (danieldaniel)
on 2006-05-26 20:51
(Received via mailing list)
The only thing I can think of is that params[:id] isn't actually being
set?
8acba6d057b3f0c2c16cca95ad36881b?d=identicon&s=25 Bakki Kudva (bakki)
on 2006-05-26 21:14
(Received via mailing list)
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
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 Brian Hughes (Guest)
on 2006-05-26 21:26
(Received via mailing list)
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
8acba6d057b3f0c2c16cca95ad36881b?d=identicon&s=25 Bakki Kudva (bakki)
on 2006-05-27 20:02
(Received via mailing list)
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
This topic is locked and can not be replied to.