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:
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?
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.
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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.