Using devise, rails 3.2. I’m trying to figure out how to create an array of ids and then check if the current_user’s id is included in this array.
My users have many locations through location_users. (Locations have many users through the same relationship.)
I can get a list of user_ids associated with a location without problem:
@location.users.each do |u|
puts u.id
However, I assume I need to output the ids to an array and check with something like this:
- [user_ids].include? current_user.id
How can I go about achieving this?
or rather just
should work, but involves loading all location.users and then searching through the array. I would consider writing a statements that lets the database handle this.
update: here’s a way how you can let the db handle it say you want to check if user 5 is in location 1:
this returns either the association (instance of LocationsUser) or nil.