# Proper way to replace ActiveRecord methods for all Models

**URL:** <https://www.ruby-forum.com/t/proper-way-to-replace-activerecord-methods-for-all-models/133851>\
**Category:** Rails\
**Created:** [April 4, 2008, 12:32pm UTC](https://www.ruby-forum.com/t/proper-way-to-replace-activerecord-methods-for-all-models/133851 "2008-04-04T12:32:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![apramanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9a140/32.png) [@apramanik](https://www.ruby-forum.com/u/apramanik)\
**Post date:** [April 4, 2008, 12:32pm UTC](https://www.ruby-forum.com/t/proper-way-to-replace-activerecord-methods-for-all-models/133851/1 "2008-04-04T12:32:34Z")

</div>

Hi all,

What is the proper way to replace/wrap functionality in ActiveRecord  
for all my models? Currently, I am doing this which works:

module ActiveRecord

module AttributeMethods

```
alias write_attribute_internal write_attribute

def write_attribute( attr_name, value )

  value = nil if value == '' # Empty strings are considered nil!

  write_attribute_internal( attr_name, value )

end

```

end

end

But it feels messy. Inheritance seems to be out of the question  
because of the way ActiveRecord infers the table name (table\_name()).

---

<div class="post-metadata">

**Author:** ![apramanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9a140/32.png) [@apramanik](https://www.ruby-forum.com/u/apramanik)\
**Post date:** [April 4, 2008, 1:57pm UTC](https://www.ruby-forum.com/t/proper-way-to-replace-activerecord-methods-for-all-models/133851/2 "2008-04-04T13:57:45Z")

</div>

On Fri, Apr 4, 2008 at 6:31 AM, apramanik [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> alias write_attribute_internal write_attribute
> 
> ```
> 
> end
> 
> But it feels messy. Inheritance seems to be out of the question  
> because of the way ActiveRecord infers the table name (table\_name()).

Leaving aside the question of why you’d want to do this globally, such  
stuff is normally done in Rails using alias\_method chain. Which would  
make this more conventional code:

module ActiveRecord::AttributeMethods  
def write\_attribute\_with\_empty\_string\_nilling( attr\_name, value )  
value = nil if value == ‘’ # Empty strings are considered nil!  
write\_attribute\_without\_empty\_string\_nilling attr\_name, value  
end

```
alias_method_chain :write_attribute :empty_string_nilling

```

end

> **[New in Rails: Module#alias\_method\_chain](https://rubyonrails.org/2006/4/26/new-in-rails-module-alias_method_chain)**
>
> Though not an outward facing feature, changeset 4276 introduces a nice method to DRY up and encapsulate the common pattern of aliasing a method so that you can build behavior on top of it.All over the internals of Rails you’ll find code like this in...

> **[alias\_method\_bling — err.the\_blog](http://errtheblog.com/posts/48-aliasmethodbling)**
>
> Err the Blog: Ruby and Rails from the front.

–  
Rick DeNatale

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

---

<div class="post-metadata">

**Author:** ![apramanik](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9a140/32.png) [@apramanik](https://www.ruby-forum.com/u/apramanik)\
**Post date:** [April 4, 2008, 8:48pm UTC](https://www.ruby-forum.com/t/proper-way-to-replace-activerecord-methods-for-all-models/133851/3 "2008-04-04T20:48:48Z")

</div>

Haha, it was just a test to see if it would work. Thanks for the info!
