Reputation: 281
Simply I have a collection_select
which allows a user to select any users that share the same business.id
. It uses the list @colleagues
created in Ruby:
#events_controller#new
@colleagues = User.where(business_id: current_user.business_id)
#events/new.html.erb
<%= f.collection_select :id,
@colleagues,
:id,
:full_name %>
But at the moment, this list includes the current_user
, which I do not want to happen (the user should not be able to select them self). Is there a way I can exclude the current_user
from this list from querying the database? I am using SQLite.
Thank you, and any further explanation would be much appreciated as I am new to Ruby and SQL.
Upvotes: 0
Views: 884
Reputation: 3060
This code works for me in rails 6.0.0
form.collection_select :parent_id, Collection.all.where.not(id: collection.id), :id, :title
Upvotes: 0
Reputation: 10406
@colleagues = User.where(business_id: current_user.business_id).where.not(id: current_user.id)
Upvotes: 3
Reputation: 30056
You might pass to collection_select something like
@colleagues.reject {|user| user.id == current_user.id }
Upvotes: 1