views:

108

answers:

1

Hi all,

Groups has_ many users. I have 2 activerecord arrays: 1. groups = containing groups with its users. 2. users = containing users attending an event.

I start by iterating over the groups. Then I iterate over the users in the second array and search for that user in the first array (see code). If the user is found, show and delete from the array.

<% groups.each do |group| %> 
<h2><%= group.title %></h2> 
  <% users.each do |user| %> 
    # I want to search for user in group.users 
    # and if present show here and remove the user from 
    # group.users (remove from the array not from the database) 
  <% end %> 
<% end %> 

I don't know how to search for a user in another active record array and then delete him. All help is greatly appreciated.

thanks

+1  A: 

Directly answering your question, i.e. displaying and deleting a user of interest from all froups' users:

<% @groups.collect { |group|
     group.users.delete_if { |user|
       user == my_user
     }
   }.flatten.each do |user| %>

   <%= ((display user)) %>

<% end %>

Alternatively,

<% @groups.each do |group| %>
   <% if user = group.delete(my_user) %>
     <%= ((display user)) %>
   <% end %>
<% end %>
vladr