# Extending a module in a controller

**URL:** <https://www.ruby-forum.com/t/extending-a-module-in-a-controller/189042>\
**Category:** Rails\
**Created:** [May 22, 2010, 11:58pm UTC](https://www.ruby-forum.com/t/extending-a-module-in-a-controller/189042 "2010-05-22T23:58:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [May 22, 2010, 11:58pm UTC](https://www.ruby-forum.com/t/extending-a-module-in-a-controller/189042/1 "2010-05-22T23:58:05Z")

</div>

I’m trying to extend Enumerable in my Rails app. I’ve started with just  
the controller where I wanted to use this method i.e.

class LookupController \< ApplicationController

module Enumerable  
def my\_compress  
…

… but when I visit the web page in question I get “No method  
‘my\_compress’ for Array …”

I’m probably doing something fundamentally wrong - what could/should I  
be doing?

thanks

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [May 23, 2010, 1:59am UTC](https://www.ruby-forum.com/t/extending-a-module-in-a-controller/189042/2 "2010-05-23T01:59:37Z")

</div>

Toby R. wrote:

> I’m trying to extend Enumerable in my Rails app. I’ve started with just  
> the controller where I wanted to use this method i.e.
> 
> class LookupController \< ApplicationController
> 
> module Enumerable  
> def my\_compress  
> …
> 
> … but when I visit the web page in question I get “No method  
> ‘my\_compress’ for Array …”
> 
> I’m probably doing something fundamentally wrong - what could/should I  
> be doing?

You should be putting your model logic in the model, not in the  
controller.

> thanks

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [May 23, 2010, 10:13am UTC](https://www.ruby-forum.com/t/extending-a-module-in-a-controller/189042/3 "2010-05-23T10:13:37Z")

</div>

On May 23, 12:59 am, Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You should be putting your model logic in the model, not in the  
> controller.

on top of that, by putting it inside the controller like that you’ve  
created a new module called LookupController::Enumerable rather than  
extending Enumerable. If I were you I’d keep extensions to core  
classes somewhere in lib.

Fred

---

<div class="post-metadata">

**Author:** ![tobyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ebca7d/32.png) [@tobyr](https://www.ruby-forum.com/u/tobyr)\
**Post date:** [May 23, 2010, 11:12am UTC](https://www.ruby-forum.com/t/extending-a-module-in-a-controller/189042/4 "2010-05-23T11:12:05Z")

</div>

Frederick C. wrote:

> On May 23, 12:59ï¿½am, Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
> … by putting it inside the controller like that you’ve  
> created a new module called LookupController::Enumerable rather than  
> extending Enumerable. If I were you I’d keep extensions to core  
> classes somewhere in lib.
> 
> Fred

Ah I see, of course. So I’ve moved it to ‘my\_extensions.rb’ in lib/,  
added “require ‘my\_extensions’” in the controller and now it works fine.  
Thanks!
