# Before\_destroy and protecting some records

**URL:** <https://www.ruby-forum.com/t/before-destroy-and-protecting-some-records/145419>\
**Category:** Rails\
**Created:** [August 9, 2008, 12:27am UTC](https://www.ruby-forum.com/t/before-destroy-and-protecting-some-records/145419 "2008-08-09T00:27:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vineire77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9c0ed/32.png) [@vineire77](https://www.ruby-forum.com/u/vineire77)\
**Post date:** [August 9, 2008, 12:27am UTC](https://www.ruby-forum.com/t/before-destroy-and-protecting-some-records/145419/1 "2008-08-09T00:27:25Z")

</div>

I want to delete some "Accounts’ that have dependent “Users”, however I  
want to skip users tagged is\_power. I want to do this by resetting the  
users to a master account id.

In my User model I have the following code:

* * *

before\_destroy :reset\_power\_users

def reset\_power\_users  
users = User.find\_by\_account\_id\_and\_is\_power(account.id, true)  
users.each {|user| update\_attribute(:account\_id, 1)}  
end

* * *

I’m not sure the variables I’m passing are correct, but it’s not  
throwing and error and deleting all users under the current account.

Thanks in advance for taking a look 🙂

---

<div class="post-metadata">

**Author:** ![vineire77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e9c0ed/32.png) [@vineire77](https://www.ruby-forum.com/u/vineire77)\
**Post date:** [August 9, 2008, 12:34am UTC](https://www.ruby-forum.com/t/before-destroy-and-protecting-some-records/145419/2 "2008-08-09T00:34:47Z")

</div>

> I’m not sure the variables I’m passing are correct, but it’s not  
> throwing and error and deleting all users under the current account.

Well at the very least it’s not working because find\_by\_xxx is  
analogous to find :first (ie returns a single record or nil). The  
find :all equivalent is find\_all\_by\_xxx

Fred
