# Drying up my controllers using inheritance

**URL:** <https://www.ruby-forum.com/t/drying-up-my-controllers-using-inheritance/69064>\
**Category:** Rails\
**Created:** [September 18, 2006, 1:14pm UTC](https://www.ruby-forum.com/t/drying-up-my-controllers-using-inheritance/69064 "2006-09-18T13:14:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![harvey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/96bed5/32.png) [@harvey](https://www.ruby-forum.com/u/harvey)\
**Post date:** [September 18, 2006, 1:14pm UTC](https://www.ruby-forum.com/t/drying-up-my-controllers-using-inheritance/69064/1 "2006-09-18T13:14:47Z")

</div>

Morning

I have three controllers, which share a lot of functionality. I was  
wonder if there was a way to keep dry using a base controller and then  
inherit from it in my other controllers.

Here some code from one of my controllers

def show  
@home = Home.find(params[:id])  
if @home.active?  
render :action =\> ‘show\_active\_true’  
else  
render :action =\> ‘show\_active\_false’  
end  
rescue ActiveRecord::RecordNotFound  
logger.error(“An attempt has been made to access a home that does not  
exist in the database. The home id was #{params[:id]}”)  
flash\_failure ‘An attempt has been made to access a home that does  
not exist in the database.’  
redirect\_to :action =\> ‘index’  
end

The show method in the other controllers are basically the same except,  
they will relate to another model. So you have @care\_agency =  
CareAgency.find(params[:id]). Basically, I just want to replace the  
word home with care\_agency in that particular controller.

Is there a nice dry way to abstract and just basically change the model?

Thanks for any pointers!  
Harvey

---

<div class="post-metadata">

**Author:** ![harvey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/96bed5/32.png) [@harvey](https://www.ruby-forum.com/u/harvey)\
**Post date:** [September 18, 2006, 11:41pm UTC](https://www.ruby-forum.com/t/drying-up-my-controllers-using-inheritance/69064/2 "2006-09-18T23:41:13Z")

</div>

On 9/18/06, harvey [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Morning

> The show method in the other controllers are basically the same except,  
> they will relate to another model. So you have @care\_agency =  
> CareAgency.find(params[:id]). Basically, I just want to replace the  
> word home with care\_agency in that particular controller.
> 
> Is there a nice dry way to abstract and just basically change the model?

Maybe this might work for you:

class BaseController \< ActionController::Base

def BaseController.model(model\_name)  
@model\_class = const\_get(Inflector.classify(model\_name))  
@model\_var = model\_name  
end

def show  
inst\_var=“@#{model\_var}”, model\_class.find(params[:id]  
instance\_variable\_set(inst\_var)  
if inst\_var.active?  
render :action =\> ‘show\_active\_true’  
else  
render :action =\> ‘show\_active\_false’  
end  
rescue ActiveRecord::RecordNotFound  
logger.error(“An attempt has been made to access a  
#{model\_class.name} that does not  
exist in the database. The #{model\_class.name} id was #{params[:id]}”)  
flash\_failure ‘An attempt has been made to access a  
#{model\_class.name} that does  
not exist in the database.’  
redirect\_to :action =\> ‘index’  
end

end

And:

class HomeController \< BaseController  
model :home  
end

Cheers,  
Max

---

<div class="post-metadata">

**Author:** ![harvey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/96bed5/32.png) [@harvey](https://www.ruby-forum.com/u/harvey)\
**Post date:** [September 19, 2006, 9:49pm UTC](https://www.ruby-forum.com/t/drying-up-my-controllers-using-inheritance/69064/3 "2006-09-19T21:49:26Z")

</div>

Harvey,

If there’s a lot of duplication among your controllers and other  
objects, consider consolidating the data itself. For example, “home” and  
“care\_agency” look like two permutations of the same entity - maybe a  
“facility” table with a column for facility\_type would be a simpler way  
to go.

Shauna

---

<div class="post-metadata">

**Author:** ![harvey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/96bed5/32.png) [@harvey](https://www.ruby-forum.com/u/harvey)\
**Post date:** [September 19, 2006, 4:17pm UTC](https://www.ruby-forum.com/t/drying-up-my-controllers-using-inheritance/69064/4 "2006-09-19T16:17:04Z")

</div>

Or you could do the same thing by mixing in a module. That seems a  
little more Ruby-like to me.

---

<div class="post-metadata">

**Author:** ![harvey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/96bed5/32.png) [@harvey](https://www.ruby-forum.com/u/harvey)\
**Post date:** [September 22, 2006, 10:22am UTC](https://www.ruby-forum.com/t/drying-up-my-controllers-using-inheritance/69064/5 "2006-09-22T10:22:30Z")

</div>

Guys  
Thanks for all the help.  
Harvey
