# Tmp delete from activerecord

**URL:** <https://www.ruby-forum.com/t/tmp-delete-from-activerecord/135342>\
**Category:** Rails\
**Created:** [April 21, 2008, 11:15am UTC](https://www.ruby-forum.com/t/tmp-delete-from-activerecord/135342 "2008-04-21T11:15:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![marlor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90ced4/32.png) [@marlor](https://www.ruby-forum.com/u/marlor)\
**Post date:** [April 21, 2008, 11:15am UTC](https://www.ruby-forum.com/t/tmp-delete-from-activerecord/135342/1 "2008-04-21T11:15:05Z")

</div>

Hello,

This is the case:  
A project has many users.  
a project has an admin.

when I list the project users I don’t want to display the admin user so  
what I tried was:

users = @project.users  
users.delete(admin\_user)

users.each { |u| {  
…  
}

but what happens is that the admin\_user gets deleted from the project.  
is there a way to do a temporary delete without activerecord acting upon  
it and without having to check with an ‘if’ inside the for loop?

thanks

---

<div class="post-metadata">

**Author:** ![marlor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90ced4/32.png) [@marlor](https://www.ruby-forum.com/u/marlor)\
**Post date:** [April 21, 2008, 11:26am UTC](https://www.ruby-forum.com/t/tmp-delete-from-activerecord/135342/2 "2008-04-21T11:26:23Z")

</div>

in your table you must have an field, that defines the role of the user.  
either an bool flag like is\_admin, that can be true or false or a  
integer or string, with several states like admin, customer and so on.

with a is\_admin flag:  
users = @project.users.find\_by\_is\_admin(false)

with an integer role (0=normal user, 1 = admin)  
users = @project.users.find\_by\_role(0)

with a string role (“admin” = admin, “user” = user…)  
users = @project.users.find\_by\_role(“user”)

---

<div class="post-metadata">

**Author:** ![marlor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90ced4/32.png) [@marlor](https://www.ruby-forum.com/u/marlor)\
**Post date:** [April 21, 2008, 11:33am UTC](https://www.ruby-forum.com/t/tmp-delete-from-activerecord/135342/3 "2008-04-21T11:33:56Z")

</div>

Hello Thorsten,

Thanks for the reply.

I though of that suggestion but the problem is that there is a  
“permissions” table in between user and project which keeps the ‘admin’  
boolean.  
Yes it can be queried as well but I thought that would be just  
performance loss just to filter out one user.  
Getting all users of a project and just subtracting the admin\_user  
(which I already have) seemed a better solution.

Is there really no way for this similar to the way i did it:  
users = @project.users  
users.delete(admin\_user)  
but then without actually deleting the record from DB?

Thanks

---

<div class="post-metadata">

**Author:** ![marlor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90ced4/32.png) [@marlor](https://www.ruby-forum.com/u/marlor)\
**Post date:** [April 21, 2008, 11:46am UTC](https://www.ruby-forum.com/t/tmp-delete-from-activerecord/135342/4 "2008-04-21T11:46:08Z")

</div>

Very strange, as this should work just fine. I too would expect this  
to perform an Array#delete, not an ActiveRecord::Base.delete, and  
testing this in one of my projects worked fine.

Can you verify that the result of @project.users is indeed an array?

Jan

---

<div class="post-metadata">

**Author:** ![marlor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90ced4/32.png) [@marlor](https://www.ruby-forum.com/u/marlor)\
**Post date:** [April 21, 2008, 11:59am UTC](https://www.ruby-forum.com/t/tmp-delete-from-activerecord/135342/5 "2008-04-21T11:59:40Z")

</div>

Hello Jan,

Thanks for the reply.

@project.users.class says it is an Array.

when I don’t do the ‘delete’ then the admin user does not get removed so  
this shouldn’t be a problem in another part of my code.

strange indeed

---

<div class="post-metadata">

**Author:** ![marlor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90ced4/32.png) [@marlor](https://www.ruby-forum.com/u/marlor)\
**Post date:** [April 21, 2008, 11:53am UTC](https://www.ruby-forum.com/t/tmp-delete-from-activerecord/135342/6 "2008-04-21T11:53:20Z")

</div>

hm, i see. you want to use the array delete function. i’m not sure, how  
you could get the “old” one that was overwritten by ActiveRecord.

maybe you could use delete\_if, as far as i know that’s not recycled by  
Rails.

users.delete\_if {|u| u == admin\_user }

but since it has to go through the whole array and check every single  
item, you could do it in the loop, as far as performance is concerned.

---

<div class="post-metadata">

**Author:** ![marlor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90ced4/32.png) [@marlor](https://www.ruby-forum.com/u/marlor)\
**Post date:** [April 21, 2008, 12:14pm UTC](https://www.ruby-forum.com/t/tmp-delete-from-activerecord/135342/7 "2008-04-21T12:14:32Z")

</div>

On 21 Apr 2008, at 10:45, Jan-Christian F. wrote:

> Very strange, as this should work just fine. I too would expect this  
> to perform an Array#delete, not an ActiveRecord::Base.delete, and  
> testing this in one of my projects worked fine.
> 
> Can you verify that the result of @project.users is indeed an array?

It’s not an array, it’s an association proxy.

u = @project.users  
u.delete(foo)

is the same as @projects.users.delete(foo) (indeed there would be some  
very funky going on if it wasn’t).

In this case you could do something like  
users\_without\_admin = @project.users.reject {|u| u == admin}  
or of course arrange for admin not to be in @project.users in the  
first place.

Fred

---

<div class="post-metadata">

**Author:** ![marlor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90ced4/32.png) [@marlor](https://www.ruby-forum.com/u/marlor)\
**Post date:** [April 21, 2008, 4:25pm UTC](https://www.ruby-forum.com/t/tmp-delete-from-activerecord/135342/8 "2008-04-21T16:25:38Z")

</div>

> with a is\_admin flag:  
> users = @project.users.find\_by\_is\_admin(false)

find\_all\_by\_is\_admin!

---

<div class="post-metadata">

**Author:** ![marlor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90ced4/32.png) [@marlor](https://www.ruby-forum.com/u/marlor)\
**Post date:** [April 21, 2008, 1:42pm UTC](https://www.ruby-forum.com/t/tmp-delete-from-activerecord/135342/9 "2008-04-21T13:42:56Z")

</div>

Jiminy, I didn’t know that. And of course I tested it with a regular  
finder, not with an association … my bad.

Thanks Fred!
