# Model methods

**URL:** <https://www.ruby-forum.com/t/model-methods/95065>\
**Category:** Rails\
**Created:** [May 21, 2006, 5:01am UTC](https://www.ruby-forum.com/t/model-methods/95065 "2006-05-21T05:01:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [May 21, 2006, 5:01am UTC](https://www.ruby-forum.com/t/model-methods/95065/1 "2006-05-21T05:01:52Z")

</div>

Hello everyone,

In my model, I have

```
def leave_at_formatted
	leave_at.strftime("%I:%M %p")
end

```

I was wondering if there were any way to define to\_s for leave\_at in  
order  
to get the same result?

Thanks!

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [May 21, 2006, 5:08am UTC](https://www.ruby-forum.com/t/model-methods/95065/2 "2006-05-21T05:08:09Z")

</div>

I’m afraid I may have been unclear - in the code below, leave\_at is an  
attribute of the model

> >

Hello everyone,

In my model, I have

```
def leave_at_formatted
	leave_at.strftime("%I:%M %p")
end

```

I was wondering if there were any way to define to\_s for leave\_at in  
order  
to get the same result?

Thanks!

* * *

Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [May 21, 2006, 6:30am UTC](https://www.ruby-forum.com/t/model-methods/95065/3 "2006-05-21T06:30:01Z")

</div>

On Sat, 2006-05-20 at 17:06 -1000, Daniel H. wrote:

> end
> 
> I was wondering if there were any way to define to\_s for leave\_at in order  
> to get the same result?

* * *

I’m gathering that ‘leave\_at’ is actually a column in your db and you  
don’t want to have to convert in/out when you use it.

I have the following in my ‘environment.rb’

ActiveSupport::CoreExtensions:📅:Conversions::DATE\_FORMATS.merge!(  
:default =\> ‘%m/%d/%Y’,  
:date\_time12 =\> “%m/%d/%Y %I:%M%p”,  
:date\_time24 =\> “%m/%d/%Y %H:%M”)

but that becomes a default for all usage in my application - though this  
is Date Class, I am sure that you can do similarly for Time Class.

Craig

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [May 21, 2006, 6:36am UTC](https://www.ruby-forum.com/t/model-methods/95065/4 "2006-05-21T06:36:28Z")

</div>

On Sat, May 20, 2006 at 05:06:40PM -1000, Daniel H. wrote:  
} I’m afraid I may have been unclear - in the code below, leave\_at is an  
} attribute of the model  
}  
} \>\>  
} Hello everyone,  
}  
} In my model, I have  
}  
} def leave\_at\_formatted  
} leave\_at.strftime("%I:%M %p")  
} end  
}  
} I was wondering if there were any way to define to\_s for leave\_at in  
order  
} to get the same result?

I don’t recommend it, but it _is_ possible:

module LeaveAtFormat  
def to\_s  
strftime("%I:%M %p")  
end  
end

class WhateverModel  
def leave\_at  
val = self[:leave\_at]  
class \<\< val  
include LeaveAtFormat  
end  
return val  
end  
end

You extend the individual object to define #to\_s the way you want as you  
are returning it.

} Thanks!  
–Greg

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [May 21, 2006, 8:39pm UTC](https://www.ruby-forum.com/t/model-methods/95065/5 "2006-05-21T20:39:09Z")

</div>

Ah, didn’t know you could do that, thanks!

---

<div class="post-metadata">

**Author:** ![agnathan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/919ad9/32.png) [@agnathan](https://www.ruby-forum.com/u/agnathan)\
**Post date:** [May 21, 2006, 8:38pm UTC](https://www.ruby-forum.com/t/model-methods/95065/6 "2006-05-21T20:38:56Z")

</div>

I was afraid it’d be something like that… Thanks!
