# How to DRY up my code

**URL:** <https://www.ruby-forum.com/t/how-to-dry-up-my-code/105611>\
**Category:** Rails\
**Created:** [May 31, 2007, 6:13pm UTC](https://www.ruby-forum.com/t/how-to-dry-up-my-code/105611 "2007-05-31T18:13:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [May 31, 2007, 6:13pm UTC](https://www.ruby-forum.com/t/how-to-dry-up-my-code/105611/1 "2007-05-31T18:13:12Z")

</div>

Is there a good book that really shows you how to DRY up your code?

Because I don’t want to repeat my code all over again for all the 26  
alphabetical letters.

Here is my code that I use for 1 letter for the controller Client:

def index  
@clients = @clients = Client.find :all, :conditions =\> “firstname like  
‘M%’”, :order =\> “firstname ASC”  
end

Then on my View index.rhtml I have this:

Listing Clients with First Name starting with M \<% @clients.each do |client| %\> \<%end%\>

| \<%= link\_to client.firstname, :action =\> 'show', :id =\> client.id %\> |

And after the tag I would like to have a link like this:  
\<%= link\_to ‘N’, :action =\> index :letter or something to represent my  
letter I wan to use%\>

So in my controller Client I would have:

def index  
@clients = @clients = Client.find :all, :conditions =\> “firstname like  
‘?’”, :order =\> “firstname ASC”  
end

Thanks

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [May 31, 2007, 7:47pm UTC](https://www.ruby-forum.com/t/how-to-dry-up-my-code/105611/2 "2007-05-31T19:47:51Z")

</div>

\<%= link\_to “N”, :action =\> “index”, :letter =\> “N” %\>

controller:

def index  
@clients = Client.find :all, :conditions =\> [ “firstname like ‘?%’”,  
params[:letter] ], :order =\> “firstname ASC”  
end

Jason

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [May 31, 2007, 8:39pm UTC](https://www.ruby-forum.com/t/how-to-dry-up-my-code/105611/3 "2007-05-31T20:39:43Z")

</div>

Jason R. wrote:

> \<%= link\_to “N”, :action =\> “index”, :letter =\> “N” %\>
> 
> controller:
> 
> def index  
> @clients = Client.find :all, :conditions =\> [ “firstname like ‘?%’”,  
> params[:letter] ], :order =\> “firstname ASC”  
> end
> 
> Jason  
> Ok i’ve tried that, but when I click on my link it gives me this error:

error in your SQL syntax; check the manual that corresponds to your  
MySQL server version for the right syntax to use near ‘M’%’) ORDER BY  
firstname ASC’ at line 1: SELECT \* FROM clients WHERE (firstname like  
‘‘M’%’

Thanks

---

<div class="post-metadata">

**Author:** ![turnkeytraders](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ba8739/32.png) [@turnkeytraders](https://www.ruby-forum.com/u/turnkeytraders)\
**Post date:** [May 31, 2007, 8:47pm UTC](https://www.ruby-forum.com/t/how-to-dry-up-my-code/105611/4 "2007-05-31T20:47:30Z")

</div>

Oh its ok, I fixed it, thanks
