# Help me DRY this up please

**URL:** <https://www.ruby-forum.com/t/help-me-dry-this-up-please/144136>\
**Category:** Rails\
**Created:** [July 25, 2008, 6:59pm UTC](https://www.ruby-forum.com/t/help-me-dry-this-up-please/144136 "2008-07-25T18:59:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [July 25, 2008, 6:59pm UTC](https://www.ruby-forum.com/t/help-me-dry-this-up-please/144136/1 "2008-07-25T18:59:36Z")

</div>

My app has a model called items, and items has a row called title. I use  
the titles in the url, but also other places in my views, in the page  
title, etc.

Since I use the titles in the url, I have a before\_save method that  
strips out any unfriendly characters and converts spaces to underscores,  
etc.

I made a helper (which I called gstring) that does a gsub to convert the  
underscores back to spaces, but it seems like I have to type  
gstring(item.title) a lot!

I tried to add a method to my item model called “title” that does the  
gsub to convert the underscores to spaces, but that gives me a “Stack  
level too deep” error. Like this:

def title  
self.title = self.title.gsub("\_", " ")  
end

It works if I instead call it something like title\_with\_spaces but I am  
trying to type less and not have to think about it.

Another solution that would make some sense to me would be if I could  
add my helper as a string modifier so I could use item.title.pretty but  
I am not sure how to add a helper or method that works that way. If I do  
that I would like .pretty to be available across all models in all of my  
views.

I know if I get this working, that I would then have to treat the titles  
differently (re-convert to use the underscores) when I use them in a  
link\_to, but I am going to use a helper for the link\_tos anyways, so  
that isn’t a big deal.

I am trying to avoid changing everything around so that I store them in  
the db with the spaces, then only add the underscores when I use them in  
links, as I already have a lot of them in the db with underscores, and  
it affects more of my rows than just titles, I am just using that as an  
example here.

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [July 25, 2008, 7:02pm UTC](https://www.ruby-forum.com/t/help-me-dry-this-up-please/144136/2 "2008-07-25T19:02:02Z")

</div>

I had the same problem, this helped me:

write\_attribute(“title”, title.gsub(…))

Have luck!🙂

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [July 25, 2008, 7:02pm UTC](https://www.ruby-forum.com/t/help-me-dry-this-up-please/144136/3 "2008-07-25T19:02:53Z")

</div>

PaweÅ‚ K wrote:

> I had the same problem, this helped me:
> 
> write\_attribute(“title”, title.gsub(…))
> 
> Have luck!🙂

thanks, but where does that go? in my items model?

---

<div class="post-metadata">

**Author:** ![farfignugen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@farfignugen](https://www.ruby-forum.com/u/farfignugen)\
**Post date:** [July 25, 2008, 7:05pm UTC](https://www.ruby-forum.com/t/help-me-dry-this-up-please/144136/4 "2008-07-25T19:05:32Z")

</div>

Yup, in the model 🙂

So you could make this:

def title=(str)  
write\_attribute(“title”, str.gsub("\_", " "))  
end

this so far helps with stack problem 🙂
