# Removing values from a select (drop-down) box

**URL:** <https://www.ruby-forum.com/t/removing-values-from-a-select-drop-down-box/151111>\
**Category:** Rails\
**Created:** [October 16, 2008, 11:10pm UTC](https://www.ruby-forum.com/t/removing-values-from-a-select-drop-down-box/151111 "2008-10-16T23:10:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neal\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/74df32/32.png) [@Neal\_L](https://www.ruby-forum.com/u/Neal_L)\
**Post date:** [October 16, 2008, 11:10pm UTC](https://www.ruby-forum.com/t/removing-values-from-a-select-drop-down-box/151111/1 "2008-10-16T23:10:29Z")

</div>

Hello,

I am working on a project, and on one form we have a select (drop-  
down) box where the user needs to be able to select from a list of  
clients. The box is currently being populated with:

\<%= collection\_select(:client, :id, @account.clients, :id, :name) %\>

The problem is that some of these clients may have already been  
selected, and we need to remove them from the list. Is this possible?

Thanks!

---

<div class="post-metadata">

**Author:** ![Neal\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/74df32/32.png) [@Neal\_L](https://www.ruby-forum.com/u/Neal_L)\
**Post date:** [October 16, 2008, 11:14pm UTC](https://www.ruby-forum.com/t/removing-values-from-a-select-drop-down-box/151111/2 "2008-10-16T23:14:35Z")

</div>

Do clients have a .selected property or some such that you can use to  
figure out which clients are already selected? If so you could do,  
e.g.,

@account.clients.map{|c| !c.selected}

I would think.

---

<div class="post-metadata">

**Author:** ![Neal\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/74df32/32.png) [@Neal\_L](https://www.ruby-forum.com/u/Neal_L)\
**Post date:** [October 16, 2008, 11:45pm UTC](https://www.ruby-forum.com/t/removing-values-from-a-select-drop-down-box/151111/3 "2008-10-16T23:45:41Z")

</div>

That sort of worked, it returned an array of true, false values  
corresponding to !c.selected. That did help me figure out the  
solution though. Here’s what worked:

@account.clients.map{ |c| !c.can\_edit(@unit) ? c : nil }.compact

I didn’t have a selected property, but I did have a can\_edit(unit)  
method that returns true or false. Clients that can\_edit the @unit  
are already on the list…

Only thing is, this runs a separate database query for each client for  
the can\_edit(@unit) call. Is there any way to do this in one query?

Thanks!

---

<div class="post-metadata">

**Author:** ![Neal\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/74df32/32.png) [@Neal\_L](https://www.ruby-forum.com/u/Neal_L)\
**Post date:** [October 16, 2008, 11:50pm UTC](https://www.ruby-forum.com/t/removing-values-from-a-select-drop-down-box/151111/4 "2008-10-16T23:50:57Z")

</div>

Sent from my iPhone

On 16 Oct 2008, at 22:44, Neal L [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> That sort of worked, it returned an array of true, false values  
> corresponding to !c.selected. That did help me figure out the  
> solution though. Here’s what worked:
> 
> @account.clients.map{ |c| !c.can\_edit(@unit) ? c : nil }.compact

Sounds like you’re looking for the reject function.

> I didn’t have a selected property, but I did have a can\_edit(unit)  
> method that returns true or false. Clients that can\_edit the @unit  
> are already on the list…
> 
> Only thing is, this runs a separate database query for each client for  
> the can\_edit(@unit) call. Is there any way to do this in one query?

That depends entirely on what’s in your can\_edit function.

Fred

---

<div class="post-metadata">

**Author:** ![Neal\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/74df32/32.png) [@Neal\_L](https://www.ruby-forum.com/u/Neal_L)\
**Post date:** [October 17, 2008, 5:47pm UTC](https://www.ruby-forum.com/t/removing-values-from-a-select-drop-down-box/151111/5 "2008-10-17T17:47:02Z")

</div>

> Sounds like you’re looking for the reject function.  
> Ach, right–or select maybe:

@account.clients.select{|c| c.can\_edit(@unit)}

Sorry for the bum steer.

> > I didn’t have a selected property, but I did have a can\_edit(unit)  
> > method that returns true or false. Clients that can\_edit the @unit  
> > are already on the list…
> > 
> > Only thing is, this runs a separate database query for each client for  
> > the can\_edit(@unit) call. Is there any way to do this in one query?
> 
> That depends entirely on what’s in your can\_edit function.

If your controller is doing an Account.find() to populate your @account  
var, you may be able to avoid the extra queries by throwing a :include  
=\> ‘clients’ on there…

> Fred
> 
> > Thanks!
