# Duplicted Code (DRY)

**URL:** <https://www.ruby-forum.com/t/duplicted-code-dry/77129>\
**Category:** Rails\
**Created:** [November 24, 2006, 3:12pm UTC](https://www.ruby-forum.com/t/duplicted-code-dry/77129 "2006-11-24T15:12:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [November 24, 2006, 3:12pm UTC](https://www.ruby-forum.com/t/duplicted-code-dry/77129/1 "2006-11-24T15:12:26Z")

</div>

Hi everyone

I’ve a problem. I’d like to refactoring code. I generate two controllers  
with scaffold (student\_controller, customer\_controller). Actually, I  
have some duplicated code.

How can I simplify the code of the controllers? Can I make a new  
controller (human\_controller) and move the duplicated code into these?

CustomerController \< HumanController  
StudentController \< HumanController  
HumanController \< ApplicationController

Does it give any other solutions and how can I do this?

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [November 24, 2006, 3:39pm UTC](https://www.ruby-forum.com/t/duplicted-code-dry/77129/2 "2006-11-24T15:39:23Z")

</div>

M. R. wrote:

> Hi everyone
> 
> I’ve a problem. I’d like to refactoring code. I generate two controllers  
> with scaffold (student\_controller, customer\_controller). Actually, I  
> have some duplicated code.
> 
> How can I simplify the code of the controllers? Can I make a new  
> controller (human\_controller) and move the duplicated code into these?
> 
> CustomerController \< HumanController  
> StudentController \< HumanController  
> HumanController \< ApplicationController

That will work, and inheritance is one of two commonly used techniques  
for removing duplication. The other is aggregation.

See: [Extract Superclass](http://www.refactoring.com/catalog/extractSuperclass.html)  
and  
[Extract Class](http://www.refactoring.com/catalog/extractClass.html)

Alan

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [November 24, 2006, 5:38pm UTC](https://www.ruby-forum.com/t/duplicted-code-dry/77129/3 "2006-11-24T17:38:43Z")

</div>

Alan wrote:

> M. R. wrote:
> 
> > Hi everyone
> > 
> > I’ve a problem. I’d like to refactoring code. I generate two controllers  
> > with scaffold (student\_controller, customer\_controller). Actually, I  
> > have some duplicated code.
> > 
> > How can I simplify the code of the controllers? Can I make a new  
> > controller (human\_controller) and move the duplicated code into these?
> > 
> > CustomerController \< HumanController  
> > StudentController \< HumanController  
> > HumanController \< ApplicationController
> 
> That will work, and inheritance is one of two commonly used techniques  
> for removing duplication. The other is aggregation.
> 
> See: [Extract Superclass](http://www.refactoring.com/catalog/extractSuperclass.html)  
> and  
> [Extract Class](http://www.refactoring.com/catalog/extractClass.html)
> 
> Alan

Thank you for your anwser! Both controllers has the same method  
“update”. But I have the problem with the model. How can I diffentiate  
the models Student and Customer in the HumanController, when I will find  
a person (a student or customer):

class HumanController \< ApplicationController  
def update  
#!!! Here is my problem !!!  
@student = Student.find(params[:id])  
if @student.update\_attributes(params[:student])  
flash[:notice] = ‘Student was successfully updated.’  
redirect\_to :action =\> ‘show’, :id =\> @student  
else  
render :action =\> ‘edit’  
end  
end  
end

Many thanks…

---

<div class="post-metadata">

**Author:** ![flnative](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/5daacb/32.png) [@flnative](https://www.ruby-forum.com/u/flnative)\
**Post date:** [November 24, 2006, 3:52pm UTC](https://www.ruby-forum.com/t/duplicted-code-dry/77129/4 "2006-11-24T15:52:04Z")

</div>

On Friday 24 November 2006 09:12, M. R. wrote:

> Does it give any other solutions and how can I do this?

You can use mixins:

module A  
def foo

# body

end

def bar

# body

end  
end

class AA  
include A  
end

class BB  
include AA  
end

aa = AA.new  
bb = BB.new

aa.foo # works!  
bb.foo # works!

Enjoy,  
Jim P.
