Shouldn't this work? - session[:array_of_objects].delete_if


#1

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


#2

The only thing I can think of is that params[:id] isn’t actually being
set?


#3

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


#4

On May 26, 2006, at 03:04 PM, Bakki K. 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


#5

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