Forum: Ruby on Rails Removing values from a select (drop-down) box

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.
Bbf55d9960b9905e57621d5da6727b2d?d=identicon&s=25 Neal L (Guest)
on 2008-10-16 23:10
(Received via mailing list)
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!
D6434aa0b7b350f8c3ed0119d81b2ead?d=identicon&s=25 Roy Pardee (rpardee)
on 2008-10-16 23:14
(Received via mailing list)
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.
Bbf55d9960b9905e57621d5da6727b2d?d=identicon&s=25 Neal L (Guest)
on 2008-10-16 23:45
(Received via mailing list)
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!
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-10-16 23:50
(Received via mailing list)
Sent from my iPhone

On 16 Oct 2008, at 22:44, Neal L <neal.lober@gmail.com> 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
D6434aa0b7b350f8c3ed0119d81b2ead?d=identicon&s=25 Roy Pardee (rpardee)
on 2008-10-17 17:47
(Received via mailing list)
> 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!
This topic is locked and can not be replied to.