Nicholas Alek
Nicholas Alek

Reputation: 273

Rails 4: How do I find an array of user_ids in a different table excluding current_user id?

I have a Session's table that stores user_ids and their session. I need to find all users, in a Session's table, currently logged in, are followed by a user, as in a Twitter followed, but exclude current_user id from the list. Here is my current code:

Session.where.not(:user_id => current_user).pluck(current_user.following.map(&:user_id))

Upvotes: 0

Views: 73

Answers (1)

rlarcombe
rlarcombe

Reputation: 2996

To get an array of the ids in question, try:

logged_in_user_ids = Session.where(user_id: current_user.following.map(&:id)).where.not(user_id: current_user).pluck(:user_id)

For the users themselves you might then do:

User.where(id: logged_in_user_ids)

Upvotes: 1

Related Questions