I have the following 2 models:
Projects, has_many projects
Users belong_to Projects
@project = Project.find(1)
@project.users --- outputs a lot of users
What I want to be able to do is the following: Given a list of say 3 projects (1,4,11), iterate over each project's users and build an object with all the users across the three projects, first combining, while not duplicating.
Here is what I have so far, but it's not working correctly:
@aggregate_users = Array.new
params[:project_list].split(/, ?/).each do |project|
@project_temp = Project.find(project)
@project_temp.users.each do |user|
@aggregate_users << user
end
end
Suggestions? Also, how to avoid duplicate users from being added? Thanks
.uniq!won't help you here). Perhaps it would make more sense to use has_and_belongs_to_many?