Nenzo
Nenzo

Reputation: 145

IS NOT condition for rails instance variable

I have a hash with members of an appointment and try to create a MySQL query in my rails controller. The users with the ids from the map command e.g. {1,3} should be excluded in the query. So for example @appointment.members.map{|m| m.user_id} returns {1,3,5} and I'd like to find all other users but these.

@users = User.where( "id NOT IN (?)", @appointment.members.map{|m| m.user_id} )

I'm using Rails 3.2.9 so maybe my exclude statement is wrong cause I don't get any result out of this query. The problem can't be the map array - I already tested that. Thanks in advance.

Upvotes: 0

Views: 115

Answers (2)

Kevin Smouts
Kevin Smouts

Reputation: 146

I think you just have a problem with the map method, try this:

@users = User.where( "id NOT IN (?)", @appointment.members.map(&:user_id) )

EDIT: removed the wrong syntax for future readers if any

Upvotes: 1

Nenzo
Nenzo

Reputation: 145

I solved the problem with a new instance variable:

@members = @appointment.members.all

and then

@add_users = User.where( "id NOT IN (?)", @members.map(&:user_id) )

thanks for the support

Upvotes: 0

Related Questions