# How would you manipulate a column's attributes?

**URL:** <https://www.ruby-forum.com/t/how-would-you-manipulate-a-columns-attributes/172996>\
**Category:** Rails\
**Created:** [July 30, 2009, 11:37pm UTC](https://www.ruby-forum.com/t/how-would-you-manipulate-a-columns-attributes/172996 "2009-07-30T23:37:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [July 30, 2009, 11:37pm UTC](https://www.ruby-forum.com/t/how-would-you-manipulate-a-columns-attributes/172996/1 "2009-07-30T23:37:55Z")

</div>

Is there a way to do this correctly; here’s my “Person” model:

def name  
name.gsub(“Jr.”, “Junior”)  
end

I have a “name” column in my database, and I want to replace “Jr.” with  
“Junior” every time @person.name is called.

I’m getting a “stack level is too deep” error when I put the above  
definition in my model. I figure I could just rename the method to  
something like “filtered\_name” but is there a more efficient way to do  
this?

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [July 30, 2009, 11:45pm UTC](https://www.ruby-forum.com/t/how-would-you-manipulate-a-columns-attributes/172996/2 "2009-07-30T23:45:40Z")

</div>

> Is there a way to do this correctly; here’s my “Person” model:
> 
> def name  
> name.gsub(“Jr.”, “Junior”)  
> end
> 
> I have a “name” column in my database, and I want to replace “Jr.”  
> with  
> “Junior” every time @person.name is called.

I wouldn’t do this. If you insist on it…

def name  
read\_attribute(name).to\_s.gsub(“Jr.”, “Junior”)  
end

The “to\_s” will handle cases where name is nil. Or you can rescue  
it. nil.gsub raises an error.

Anyway, I wouldn’t do it because other things might use @person.name  
and not realize there’s some extra magic going on.

I’d create another method and name it something appropriate…  
filered\_name, prefered\_name, cleansed\_name, nice\_name, expanded\_name,  
etc…

Or, create a method that works on any string called say  
“expand\_abbreviations” and then do

@person.name.expand\_abbreviations

-philip

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [July 30, 2009, 11:48pm UTC](https://www.ruby-forum.com/t/how-would-you-manipulate-a-columns-attributes/172996/3 "2009-07-30T23:48:41Z")

</div>

Awesome. Thank you Philip! The @person.name.expand\_abbreviations sounds  
like a great idea. Thanks so much. I appreciate your kind warning and  
your fantastic solution.

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [July 30, 2009, 11:49pm UTC](https://www.ruby-forum.com/t/how-would-you-manipulate-a-columns-attributes/172996/4 "2009-07-30T23:49:10Z")

</div>

On Jul 30, 5:37 pm, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## definition in my model. I figure I could just rename the method to something like “filtered\_name” but is there a more efficient way to do this?
> 
> Posted viahttp://www.ruby-forum.com/.

ActiveRecord::Base models can act like a hash here:

> **[ActiveRecord::Base](https://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002345)**
>
> Active Record Active Record objects don't specify their attributes directly, but rather infer them from the table definition with which they're linked.

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [July 31, 2009, 12:06am UTC](https://www.ruby-forum.com/t/how-would-you-manipulate-a-columns-attributes/172996/5 "2009-07-31T00:06:09Z")

</div>

Hi Bob,

On Thu, 2009-07-30 at 23:37 +0200, Bob S. wrote:

> Is there a way to do this correctly; here’s my “Person” model:
> 
> def name  
> name.gsub(“Jr.”, “Junior”)  
> end
> 
> I have a “name” column in my database, and I want to replace “Jr.” with  
> “Junior” every time @person.name is called.

Philip’s recommendations are spot on, especially his caution re: ‘other  
things might use @person.name’. If you’re only interested in what’s  
displayed on the screen, you could also use a helper to do this.

Just a thought.

Bill
