# Overriding active record methods

**URL:** <https://www.ruby-forum.com/t/overriding-active-record-methods/185984>\
**Category:** Rails\
**Created:** [March 30, 2010, 4:34am UTC](https://www.ruby-forum.com/t/overriding-active-record-methods/185984 "2010-03-30T04:34:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 30, 2010, 4:34am UTC](https://www.ruby-forum.com/t/overriding-active-record-methods/185984/1 "2010-03-30T04:34:58Z")

</div>

Hi,

My application is running fine but now I had an requirement like  
when somebody tries to delete the record from database using delete or  
delete\_all I should not delete the record rather I should set the  
delete flag to 1. There are many model objects and there are many  
places I am using delete and delete\_all method. I dont want to touch  
all the places rather I want to override the delete method of  
ActiveRecord::Base and also when application tries to get the records  
from database it should get only records those the delete flag set to  
0. For this I have to override the base find method too. But I dont  
know how to override and where to override, and I dont want to add the  
overridden delete method in all the model objects at the same time.  
Please suggest the best solution for this. I am almost struck with  
this. please please help me on this…

Thanks,  
Ramu.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 30, 2010, 9:17am UTC](https://www.ruby-forum.com/t/overriding-active-record-methods/185984/2 "2010-03-30T09:17:39Z")

</div>

anyone knows about this??

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 30, 2010, 9:41am UTC](https://www.ruby-forum.com/t/overriding-active-record-methods/185984/3 "2010-03-30T09:41:04Z")

</div>

On 30 March 2010 03:27, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 1. For this I have to override the base find method too. But I dont  
> know how to override and where to override, and I dont want to add the  
> overridden delete method in all the model objects at the same time.  
> Please suggest the best solution for this. I am almost struck with  
> this. please please help me on this…

Have a look at acts\_as\_paranoid

Colin

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 30, 2010, 9:42am UTC](https://www.ruby-forum.com/t/overriding-active-record-methods/185984/4 "2010-03-30T09:42:15Z")

</div>

Maybe you could find some plugins, but i could not remember which plugin  
could do this. sorry

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 30, 2010, 9:46am UTC](https://www.ruby-forum.com/t/overriding-active-record-methods/185984/5 "2010-03-30T09:46:21Z")

</div>

On 30 March 2010 03:27, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 1. For this I have to override the base find method too. But I dont  
> know how to override and where to override, and I dont want to add the  
> overridden delete method in all the model objects at the same time.  
> Please suggest the best solution for this. I am almost struck with  
> this. please please help me on this…

If you’ve used Model.find(:all, …) you’re probably going to be a  
little stuck [1] , but if you’ve used Model.all, you can override this  
as a custom named scope in your model.

named\_scope :all, :conditions =\> “model\_table.deleted\_flag = false”

Alternatively, maybe "default\_scope :conditions =\> { :deleted\_flag =\>  
false } " would be better for you. But you’d need to be aware of using  
“with\_exclusive\_scope” if you want to chain scopes and return the  
deleted records too.

[1] You can do something along the lines of  
[http://www.freezzo.com/2008/05/14/override-default-find-conditions-for-model/](http://www.freezzo.com/2008/05/14/override-default-find-conditions-for-model/),  
but that makes it difficult to do a real “find all” - you’d probably  
have to alias the original method to a new name and remember to use it

- not really conventional…
