S Craig
S Craig

Reputation: 281

How to exclude a certain selection from a collection_select list?

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

Answers (3)

Sajidur Rahman
Sajidur Rahman

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

j-dexx
j-dexx

Reputation: 10406

@colleagues = User.where(business_id: current_user.business_id).where.not(id: current_user.id)

Upvotes: 3

Ursus
Ursus

Reputation: 30056

You might pass to collection_select something like

@colleagues.reject {|user| user.id == current_user.id }

Upvotes: 1

Related Questions