Forum: Ruby on Rails finding and deleting in activerecord array

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
82a578052d304559f15180bdf56555d2?d=identicon&s=25 Tarscher (Guest)
on 2009-03-10 13:30
(Received via mailing list)
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 hime and delete him 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 find a user in another active record array and
then delete him.

All help is greatly appreciated.

thanks
Stijn
4cd3615da7c39beacaaab174267ad8b2?d=identicon&s=25 Davi Vidal (Guest)
on 2009-03-10 13:48
(Received via mailing list)
On 03/10/2009 09:29 AM, Tarscher wrote:
> Hi all,
>
> Groups has_ many users.
>

  If one Group has_many Users, then you can access group[1]'s users
using
the group's method 'users'.

group = Group.find(1)
users_group_one = group.users

>    <% 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 %>
>

<% groups.each do |group| %>
   <% group.users.each do |user| %>
   <% end %>
<% end %>

  HTH,

davi
82a578052d304559f15180bdf56555d2?d=identicon&s=25 Tarscher (Guest)
on 2009-03-10 13:55
(Received via mailing list)
I know how to access the users but  I don't know how to search in the
array (not the database) and then delete a user if a match is found.
4cd3615da7c39beacaaab174267ad8b2?d=identicon&s=25 Davi Vidal (Guest)
on 2009-03-10 16:28
(Received via mailing list)
On 03/10/2009 09:54 AM, Tarscher wrote:
> I know how to access the users but  I don't know how to search in the
> array (not the database) and then delete a user if a match is found.
>

  So, provide examples and don't do top-posting.

group: 1
user: 1

group = Group.find(1)
user = group.users.find(1)
user.delete

  HTH,

davi
This topic is locked and can not be replied to.