Given two requests, for example:
@users1 = Users.find_by_company_id(2)
@users2 = Users.find_by_office_id(2)
I want to combine two:
@users_to_show = @users1 + @users2
The problem is how to prevent duplicate users from showing. Is there a way to combine the two (array?), And then make sure duplicate entries are deleted?
thank
UPDATED:
@project_ids = @projects.map(&:project_id)
@users = User.find_by_sql [
"SELECT DISTINCT users.*
FROM users
INNER JOIN permissions ON permissions.user_id = users.id
WHERE project_id IN (?) AND permissions.user_id != ?
UNION ALL
SELECT DISTINCT users.*
FROM users
WHERE instance_id = ?",
@project_ids, current_user.id, current_user.instance_id
]
source
share