# Shouldn't this work? - session\[:array\_of\_objects\].delete\_if

**URL:** <https://www.ruby-forum.com/t/shouldnt-this-work-session-array-of-objects-delete-if/61742>\
**Category:** Rails\
**Created:** [May 26, 2006, 8:22pm UTC](https://www.ruby-forum.com/t/shouldnt-this-work-session-array-of-objects-delete-if/61742 "2006-05-26T20:22:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bakki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6a8cbe/32.png) [@bakki](https://www.ruby-forum.com/u/bakki)\
**Post date:** [May 26, 2006, 8:22pm UTC](https://www.ruby-forum.com/t/shouldnt-this-work-session-array-of-objects-delete-if/61742/1 "2006-05-26T20:22:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bakki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6a8cbe/32.png) [@bakki](https://www.ruby-forum.com/u/bakki)\
**Post date:** [May 26, 2006, 8:51pm UTC](https://www.ruby-forum.com/t/shouldnt-this-work-session-array-of-objects-delete-if/61742/2 "2006-05-26T20:51:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bakki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6a8cbe/32.png) [@bakki](https://www.ruby-forum.com/u/bakki)\
**Post date:** [May 26, 2006, 9:14pm UTC](https://www.ruby-forum.com/t/shouldnt-this-work-session-array-of-objects-delete-if/61742/3 "2006-05-26T21:14:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bakki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6a8cbe/32.png) [@bakki](https://www.ruby-forum.com/u/bakki)\
**Post date:** [May 26, 2006, 9:26pm UTC](https://www.ruby-forum.com/t/shouldnt-this-work-session-array-of-objects-delete-if/61742/4 "2006-05-26T21:26:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bakki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/6a8cbe/32.png) [@bakki](https://www.ruby-forum.com/u/bakki)\
**Post date:** [May 27, 2006, 8:02pm UTC](https://www.ruby-forum.com/t/shouldnt-this-work-session-array-of-objects-delete-if/61742/5 "2006-05-27T20:02:23Z")

</div>

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
