# Deactivate instances on delete

**URL:** <https://www.ruby-forum.com/t/deactivate-instances-on-delete/110774>\
**Category:** Rails\
**Created:** [July 30, 2007, 7:59pm UTC](https://www.ruby-forum.com/t/deactivate-instances-on-delete/110774 "2007-07-30T19:59:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lundie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ac8455/32.png) [@lundie](https://www.ruby-forum.com/u/lundie)\
**Post date:** [July 30, 2007, 7:59pm UTC](https://www.ruby-forum.com/t/deactivate-instances-on-delete/110774/1 "2007-07-30T19:59:23Z")

</div>

Hi,

I have created an application in which a customer model has many  
locations. Customers are then assigned to an order, and one of their  
locations is also assigned to the order.

My issue comes if a locations is removed from the customer, that was  
assigned to an order. I have added an active field to the locations  
table, and when I “delete” the location, it sets it to false.

My question is how to do the find statements correctly. Should I be  
overwriting the find method within the locations model to only return  
active locations? Then if I do that, and the order.location method is  
called, how do I get it to return the old location that is not active.

Am I going about this the right way or is there a more efficient way  
to handle it?

---

<div class="post-metadata">

**Author:** ![lundie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ac8455/32.png) [@lundie](https://www.ruby-forum.com/u/lundie)\
**Post date:** [July 30, 2007, 11:01pm UTC](https://www.ruby-forum.com/t/deactivate-instances-on-delete/110774/2 "2007-07-30T23:01:05Z")

</div>

lundie wrote:

> Hi,
> 
> I have created an application in which a customer model has many  
> locations. Customers are then assigned to an order, and one of their  
> locations is also assigned to the order.
> 
> My issue comes if a locations is removed from the customer, that was  
> assigned to an order. I have added an active field to the locations  
> table, and when I “delete” the location, it sets it to false.
> 
> My question is how to do the find statements correctly. Should I be  
> overwriting the find method within the locations model to only return  
> active locations? Then if I do that, and the order.location method is  
> called, how do I get it to return the old location that is not active.
> 
> Am I going about this the right way or is there a more efficient way  
> to handle it?

I don’t see why you need to override anything, if you only are  
interested in active locations then you can always add that requirement  
to your find conditions. In this way, you will not impact your Order  
processing at all as it can still contain “deleted” addresses.

hth

ilan

---

<div class="post-metadata">

**Author:** ![lundie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ac8455/32.png) [@lundie](https://www.ruby-forum.com/u/lundie)\
**Post date:** [July 30, 2007, 11:57pm UTC](https://www.ruby-forum.com/t/deactivate-instances-on-delete/110774/3 "2007-07-30T23:57:04Z")

</div>

On 7/30/07, lundie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> My question is how to do the find statements correctly. Should I be  
> overwriting the find method within the locations model to only return  
> active locations? Then if I do that, and the order.location method is  
> called, how do I get it to return the old location that is not active.
> 
> Am I going about this the right way or is there a more efficient way  
> to handle it?

I have an old acts\_as\_paranoid plugin that does this. It overrides  
#destroy and tries to override #find so that the use of the deleted\_at  
field is transparent. Problem is, it tends to break down in certain  
edge cases. I recommend you keep it a little more explicit and use  
the scope\_out plugin.

[http://svn.techno-weenie.net/projects/plugins/acts\_as\_paranoid/](http://svn.techno-weenie.net/projects/plugins/acts_as_paranoid/)  
[http://code.google.com/p/scope-out-rails/](http://code.google.com/p/scope-out-rails/)

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![lundie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ac8455/32.png) [@lundie](https://www.ruby-forum.com/u/lundie)\
**Post date:** [July 31, 2007, 3:12pm UTC](https://www.ruby-forum.com/t/deactivate-instances-on-delete/110774/4 "2007-07-31T15:12:38Z")

</div>

Thanks!

scope-out looks like it will do the trick.
