# DRYing my app

**URL:** <https://www.ruby-forum.com/t/drying-my-app/144171>\
**Category:** Rails\
**Created:** [July 26, 2008, 4:46am UTC](https://www.ruby-forum.com/t/drying-my-app/144171 "2008-07-26T04:46:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 26, 2008, 4:46am UTC](https://www.ruby-forum.com/t/drying-my-app/144171/1 "2008-07-26T04:46:19Z")

</div>

Hello.

I am using auto-complete fields in my application and I found myself  
duplicating a lot of code.

Here is a sample of what I have in my view:

```
        <%= text_field_with_auto_complete :model,
          :codeA,
          :maxlength => 35,
          :size => 35,
          :frequency => 0,
          :style => 'off',
          :autocomplete => 'off' -%>
        <div class=auto_complete id=model_codeA_auto_complete

```

style=‘display: none’\>

```
        <%= text_field_with_auto_complete :model,
          :codeB,
          :maxlength => 35,
          :size => 35,
          :frequency => 0,
          :style => 'off',
          :autocomplete => 'off' -%>
        <div class=auto_complete id=model_codeB_auto_complete

```

style=‘display: none’\>

The models all look the same too:

class ModelA \< ActiveRecord::Base  
self.table\_name = ‘modela’

def self.get\_codes  
return ModelA.find(  
:all,  
:select =\> ‘code’,  
:order =\> ‘code’  
).collect {|row| row.code}  
end  
end

class ModelB \< ActiveRecord::Base  
self.table\_name = ‘modelb’

def self.get\_codes  
return ModelB.find(  
:all,  
:select =\> ‘code’,  
:order =\> ‘code’  
).collect {|row| row.code}  
end  
end

: : : and so on…

The only thing that changes in the methods is the name of the model,  
as you can see.

In my controller I have another set of methods to retrieve the  
necessary information for the drop downs and auto-completes:

def auto\_complete\_for\_codeA  
@codes = ModelA.get\_codes  
render\_options(@codes, params[:model][:codeA])  
end

def auto\_complete\_for\_codeB  
@codes = ModelB.get\_codes  
render\_options(@codes, params[:model][:codeB])  
end

: : : and so on…

‘render\_options’ is another method that I got DRY enough and I don’t  
need to make any changes as of right now. It finds matching values in  
@codes based on the second parameter value, then renders a partial.

As you can see there is a lot of duplication in all of this and I  
would like to make it as DRY as possible. The only thing I think can  
be done is to to modify all those controller methods by soft-coding  
somehow the name of the Model to be used and get something like this:

def auto\_complete\_for\_codeA  
render\_options(:ModelA, params[:model][:codeA])  
end

def auto\_complete\_for\_codeB  
render\_options(:ModelB, params[:model][:codeB])  
end

Then render\_option would do all the grunt work converting :myModelx to  
a usable model name. If you know how to make this happen and/or have  
better ideas please let me know.

Thanks a lot.

Pepe

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 29, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/drying-my-app/144171/2 "2008-07-29T15:43:27Z")

</div>

Sorry no solutions here, but a couple of thoughts that might end up  
sending you in the right direction. I’m still new to rails and haven’t  
actually implemented anything like what you’re describing.

If modelA & modelB only differ by an attribute, maybe inheritance is  
your answer…So something like:  
class baseModel  
basic\_stuff\_here  
class modelA \< baseModel  
code method # returns codeA  
class modelB \< baseModel  
code method # returns codeB

Then I’m wondering if the rails framework would support (I suspect  
somehow with routing it does) creating just the basemodel controller/  
view. In there everything would be ‘the same’ just using the inherited  
methods.

Obviously this is an idea of where to go…I’m sure there are  
stumbling blocks on the way.  
-Dale

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [July 31, 2008, 3:11pm UTC](https://www.ruby-forum.com/t/drying-my-app/144171/3 "2008-07-31T15:11:08Z")

</div>

Hi Dale,

Sorry for the delay in responding. I’ve been quite busy these past  
days.

Thanks for the idea. I think I understand the underlying purpose.  
However, since nobody was answering this posting I decided to create a  
new one with a simpler question and I got a few answers that really  
helped me. I have not tried them yet but if you are interested, here  
is the link:

[http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/51e90f981ca81f6a/afff04e37620922e?hl=en&lnk=gst&q=soft-coding#afff04e37620922e](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/51e90f981ca81f6a/afff04e37620922e?hl=en&lnk=gst&q=soft-coding#afff04e37620922e)

Thanks again.

Pepe
