# Finding and deleting in activerecord array

**URL:** <https://www.ruby-forum.com/t/finding-and-deleting-in-activerecord-array/162485>\
**Category:** Rails\
**Created:** [March 10, 2009, 1:30pm UTC](https://www.ruby-forum.com/t/finding-and-deleting-in-activerecord-array/162485 "2009-03-10T13:30:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [March 10, 2009, 1:30pm UTC](https://www.ruby-forum.com/t/finding-and-deleting-in-activerecord-array/162485/1 "2009-03-10T13:30:37Z")

</div>

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

## \<%= group.title %\>
 \<% 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

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [March 10, 2009, 1:48pm UTC](https://www.ruby-forum.com/t/finding-and-deleting-in-activerecord-array/162485/2 "2009-03-10T13:48:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [March 10, 2009, 1:55pm UTC](https://www.ruby-forum.com/t/finding-and-deleting-in-activerecord-array/162485/3 "2009-03-10T13:55:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [March 10, 2009, 4:28pm UTC](https://www.ruby-forum.com/t/finding-and-deleting-in-activerecord-array/162485/4 "2009-03-10T16:28:23Z")

</div>

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
