# How to subclass active record to call a method on all save and destroy actions

**URL:** <https://www.ruby-forum.com/t/how-to-subclass-active-record-to-call-a-method-on-all-save-and-destroy-actions/118208>\
**Category:** Rails\
**Created:** [October 24, 2007, 8:30pm UTC](https://www.ruby-forum.com/t/how-to-subclass-active-record-to-call-a-method-on-all-save-and-destroy-actions/118208 "2007-10-24T20:30:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![teesea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7feea3/32.png) [@teesea](https://www.ruby-forum.com/u/teesea)\
**Post date:** [October 24, 2007, 8:30pm UTC](https://www.ruby-forum.com/t/how-to-subclass-active-record-to-call-a-method-on-all-save-and-destroy-actions/118208/1 "2007-10-24T20:30:46Z")

</div>

I’m looking for a way to subclass active record (I’ll refer to the  
subclass I want to create as NewActiveRecord) so that I can call  
different methods on a save or destroy action on all models which sub  
class NewActiveRecord

I dont want to use before\_save or any of the other regular call back  
methods as I want to be able to use these call backs in the models  
which subclass NewActiveRecord without having to call super everytime  
I use them.

Anyone have any ideas about how I can do this?

Cheers

Tony

---

<div class="post-metadata">

**Author:** ![teesea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7feea3/32.png) [@teesea](https://www.ruby-forum.com/u/teesea)\
**Post date:** [October 24, 2007, 8:35pm UTC](https://www.ruby-forum.com/t/how-to-subclass-active-record-to-call-a-method-on-all-save-and-destroy-actions/118208/2 "2007-10-24T20:35:27Z")

</div>

On 10/24/07, teesea [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m looking for a way to subclass active record (I’ll refer to the  
> subclass I want to create as NewActiveRecord) so that I can call  
> different methods on a save or destroy action on all models which sub  
> class NewActiveRecord
> 
> I dont want to use before\_save or any of the other regular call back  
> methods as I want to be able to use these call backs in the models  
> which subclass NewActiveRecord without having to call super everytime  
> I use them.

Use ActiveRecord::Observer instead of ActiveRecord::Callbacks?

[http://www.robbyonrails.com/articles/2007/04/28/q-a-activerecord-observers-and-you](http://www.robbyonrails.com/articles/2007/04/28/q-a-activerecord-observers-and-you)  
[http://api.rubyonrails.org/classes/ActiveRecord/Observer.html](http://api.rubyonrails.org/classes/ActiveRecord/Observer.html)

---

<div class="post-metadata">

**Author:** ![teesea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7feea3/32.png) [@teesea](https://www.ruby-forum.com/u/teesea)\
**Post date:** [October 24, 2007, 9:35pm UTC](https://www.ruby-forum.com/t/how-to-subclass-active-record-to-call-a-method-on-all-save-and-destroy-actions/118208/3 "2007-10-24T21:35:10Z")

</div>

You probably want to set self.abstract\_class = true.

Fred

---

<div class="post-metadata">

**Author:** ![teesea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7feea3/32.png) [@teesea](https://www.ruby-forum.com/u/teesea)\
**Post date:** [October 25, 2007, 10:34am UTC](https://www.ruby-forum.com/t/how-to-subclass-active-record-to-call-a-method-on-all-save-and-destroy-actions/118208/4 "2007-10-25T10:34:31Z")

</div>

Thanks for the observer link, they would work nicely but I need to be  
able to stop a model from saving or destroying, if the additional  
method i call returns false.

Basically what I’m trying to do is add some authentication methods  
into all my active record models so that whenever a save or destroy,  
and also a association is added the authentication method checks that  
the user has privileges to do this.

@Frederick

Thanks, I’ve heard that using self.abstract\_class = true will allow me  
to sub class ActiveRecord, I need to know which single method in  
ActiveRecord is best to override to add the authentication methods. Do  
you know which would be best?, or could you point me in the direction  
of some good resources on ActiveRecord

Cheers

Tony

On Oct 24, 8:34 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![teesea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7feea3/32.png) [@teesea](https://www.ruby-forum.com/u/teesea)\
**Post date:** [October 25, 2007, 7:48pm UTC](https://www.ruby-forum.com/t/how-to-subclass-active-record-to-call-a-method-on-all-save-and-destroy-actions/118208/5 "2007-10-25T19:48:11Z")

</div>

On 10/25/07, teesea [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> could you point me in the direction  
> of some good resources on ActiveRecord

For extending ActiveRecord the fairly new “Pro ActiveRecord” book from  
APress is pretty good.

## I’m not sure I’d recommend it for an introduction/only reference to AR (AWDR 2ed does a better job IMHO) but for advanced things like extending AR, it’s the best resource I’ve found as yet.

Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![teesea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7feea3/32.png) [@teesea](https://www.ruby-forum.com/u/teesea)\
**Post date:** [October 28, 2007, 11:18am UTC](https://www.ruby-forum.com/t/how-to-subclass-active-record-to-call-a-method-on-all-save-and-destroy-actions/118208/6 "2007-10-28T11:18:57Z")

</div>

Thanks to all those who responded

I’ll have a look at acts\_as\_paranoid and see what the best way forward  
is

---

<div class="post-metadata">

**Author:** ![teesea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7feea3/32.png) [@teesea](https://www.ruby-forum.com/u/teesea)\
**Post date:** [October 25, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/how-to-subclass-active-record-to-call-a-method-on-all-save-and-destroy-actions/118208/7 "2007-10-25T22:40:13Z")

</div>

We don’t do that kinda stuff in the Ruby world.

We use modules instead, so we can “mix in” that behavior.

In lib/ar\_extentions.rb

module MyActiveRecordExtentions

def save(\*args)  
if some\_condition  
super(\*args) # do the original  
else  
return false # or do your own stuff  
end  
end

def destroy  
return false  
end

end

Then all I need to do is

class User \< ActiveRecord::Base

include MyActiveRecordExtensions

end

If you really want to see how this works in action, take a look at the  
acts\_as\_paranoid plugin. It uses this technique to mark records as  
deleted  
instead of actually deleting them, by overriding the destroy method.  
It’s a  
good place to start learning about this kinda stuff.
