# Extend ApplicationController via lib code

**URL:** <https://www.ruby-forum.com/t/extend-applicationcontroller-via-lib-code/137684>\
**Category:** Rails\
**Created:** [May 15, 2008, 9:46am UTC](https://www.ruby-forum.com/t/extend-applicationcontroller-via-lib-code/137684 "2008-05-15T09:46:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![goodwill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ac8455/32.png) [@goodwill](https://www.ruby-forum.com/u/goodwill)\
**Post date:** [May 15, 2008, 9:46am UTC](https://www.ruby-forum.com/t/extend-applicationcontroller-via-lib-code/137684/1 "2008-05-15T09:46:45Z")

</div>

I have come across a problem when I am trying to extend  
ApplicationController with a .rb file containing a module inside /lib  
folder.

The problem is, let’s say if I want to add a method called  
authenticate inside ApplicationController using  
ApplicationController.send(:include), I would do it as:

extension.rb  
module Extension  
module ClassMethods  
…  
end  
def self.included(base)  
base.extend(Extension.ClassMethods)  
end  
end

ApplicationController.send(:include, Extension)

This would work only if I put the require statement at the end of  
application.rb, I want to know if there is any chance it would work  
too if I want to put the require statement at the top?

---

<div class="post-metadata">

**Author:** ![goodwill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ac8455/32.png) [@goodwill](https://www.ruby-forum.com/u/goodwill)\
**Post date:** [May 15, 2008, 11:10am UTC](https://www.ruby-forum.com/t/extend-applicationcontroller-via-lib-code/137684/2 "2008-05-15T11:10:22Z")

</div>

On 15 May 2008, at 08:45, goodwill wrote:

> module Extension  
> This would work only if I put the require statement at the end of  
> application.rb, I want to know if there is any chance it would work  
> too if I want to put the require statement at the top?

Have a think: if you do that you’re trying to something to application  
controller even though it hasn’t been defined yet.

Fred

---

<div class="post-metadata">

**Author:** ![goodwill](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ac8455/32.png) [@goodwill](https://www.ruby-forum.com/u/goodwill)\
**Post date:** [May 17, 2008, 4:49am UTC](https://www.ruby-forum.com/t/extend-applicationcontroller-via-lib-code/137684/3 "2008-05-17T04:49:02Z")

</div>

Thats why its a difficult question 🙂  
I managed to fix it by extend ActionController::Base instead, I have  
seen plugins doing that.

On May 15, 5:09 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
