# How to add custom methods to ActionController::Base, ActiveRecord::Base

**URL:** <https://www.ruby-forum.com/t/how-to-add-custom-methods-to-actioncontroller-base-activerecord-base/133263>\
**Category:** Rails\
**Created:** [March 29, 2008, 3:19pm UTC](https://www.ruby-forum.com/t/how-to-add-custom-methods-to-actioncontroller-base-activerecord-base/133263 "2008-03-29T15:19:21Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![frank\_programmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/958977/32.png) [@frank\_programmer](https://www.ruby-forum.com/u/frank_programmer)\
**Post date:** [March 29, 2008, 3:19pm UTC](https://www.ruby-forum.com/t/how-to-add-custom-methods-to-actioncontroller-base-activerecord-base/133263/1 "2008-03-29T15:19:21Z")

</div>

I want to add custom methods to ActionController::Base,  
ActiveRecord::Base so that everything that extends it will have access  
to these methods in my Rails app. What is the best practice for doing  
this? I know I can create a file in lib and require it all the time  
but it seems like there should be a better way.

For example:

class ActionController::Base

def controller\_path  
self.class.controller\_path  
end

end

class ActiveRecord::Base  
def foo\_id(prefix=nil)  
display\_id = new\_record? ? “new” : id  
prefix ||= self.class.name.underscore  
prefix != :bare ? “#{prefix.to\_s}\_#{display\_id}” : display\_id  
end  
end

Thanks,  
Frank K.
