# Override to\_string for single attribute?

**URL:** <https://www.ruby-forum.com/t/override-to-string-for-single-attribute/98459>\
**Category:** Rails\
**Created:** [July 14, 2006, 9:46pm UTC](https://www.ruby-forum.com/t/override-to-string-for-single-attribute/98459 "2006-07-14T21:46:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [July 14, 2006, 9:46pm UTC](https://www.ruby-forum.com/t/override-to-string-for-single-attribute/98459/1 "2006-07-14T21:46:41Z")

</div>

I have a model where I want to change the display for a single attribute  
such that if the value is an integer, it displays with no decimal point  
(e.g. “1”), but if it is non-integer, it displays with a decimal point  
(e.g. “1.25”).

The easiest way to do this seems to be if I could override the to\_string  
method for my\_model.my\_attribute.to\_s. Is there a way to do this? Or  
a better way altogether?

thanks,  
Jeff

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [July 14, 2006, 10:07pm UTC](https://www.ruby-forum.com/t/override-to-string-for-single-attribute/98459/2 "2006-07-14T22:07:27Z")

</div>

On Friday, July 14, 2006, at 9:46 PM, Jeff C. wrote:

> Jeff
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

override the attribute method on your model and have it return the  
formatted value.

\_Kevin

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [July 14, 2006, 11:22pm UTC](https://www.ruby-forum.com/t/override-to-string-for-single-attribute/98459/3 "2006-07-14T23:22:56Z")

</div>

How do you do that without making it recursive? For example

#override method for attribute foo  
def foo  
return format(foo)  
end

-jeff

> override the attribute method on your model and have it return the  
> formatted value.
> 
> \_Kevin

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [July 14, 2006, 11:53pm UTC](https://www.ruby-forum.com/t/override-to-string-for-single-attribute/98459/4 "2006-07-14T23:53:34Z")

</div>

Jeff C. wrote:

> > override the attribute method on your model and have it return the  
> > formatted value.
> > 
> > \_Kevin

> How do you do that without making it recursive? For example
> 
> #override method for attribute foo  
> def foo  
> return format(foo)  
> end
> 
> -jeff

You can use read\_attribute for this.

eg the example from the documentation:

class Song \< ActiveRecord::Base  
# Uses an integer of seconds to hold the length of the song

```
 def length=(minutes)
   write_attribute(:length, minutes * 60)
 end

 def length
   read_attribute(:length) / 60
 end

```

end

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

under ‘Overwriting default accessors’

hth
