# Text\_field doesn't call overridden ActiveRecord getters

**URL:** <https://www.ruby-forum.com/t/text-field-doesnt-call-overridden-activerecord-getters/96968>\
**Category:** Rails\
**Created:** [July 3, 2006, 9:27pm UTC](https://www.ruby-forum.com/t/text-field-doesnt-call-overridden-activerecord-getters/96968 "2006-07-03T21:27:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 3, 2006, 9:27pm UTC](https://www.ruby-forum.com/t/text-field-doesnt-call-overridden-activerecord-getters/96968/1 "2006-07-03T21:27:29Z")

</div>

All,

In a template, I have

\<%= text\_field :target\_list, :DateReceived,  
{ :title =\> ‘uploaded\_at’,  
:class =\>  
‘target\_list\_info’,  
:disabled =\> ‘true’ } %\>

Here is the DateReceived method on my ActiveRecord descendant, the  
target list object:

public  
def DateReceived  
read\_attribute(‘DateReceived’).strftime(’%m/%d/%Y %I:%M:%S %p’)  
end

However, when I attempt to render this in my template, the default  
formatted database value of the DateReceived timestamp column is  
displayed instead of my specially formatted version that I return from  
the DateReceived method.

Does text\_field generate code that bypasses overridden getter methods  
for an ActiveRecord object?

Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 3, 2006, 9:50pm UTC](https://www.ruby-forum.com/t/text-field-doesnt-call-overridden-activerecord-getters/96968/2 "2006-07-03T21:50:39Z")

</div>

Wes G. wrote:

> All,
> 
> In a template, I have
> 
> \<%= text\_field :target\_list, :DateReceived,  
> { :title =\> ‘uploaded\_at’,  
> :class =\>  
> ‘target\_list\_info’,  
> :disabled =\> ‘true’ } %\>
> 
> Here is the DateReceived method on my ActiveRecord descendant, the  
> target list object:
> 
> public  
> def DateReceived  
> read\_attribute(‘DateReceived’).strftime(’%m/%d/%Y %I:%M:%S %p’)  
> end
> 
> However, when I attempt to render this in my template, the default  
> formatted database value of the DateReceived timestamp column is  
> displayed instead of my specially formatted version that I return from  
> the DateReceived method.
> 
> Does text\_field generate code that bypasses overridden getter methods  
> for an ActiveRecord object?
> 
> Wes

I can get around this by naming the method above with a different name  
than the column name.

Still, that bugs me. If the text\_field code is simply calling a method  
on the ActiveRecord object, shouldn’t what I did work?

I don’t think it matters, but template that the text\_field is in is a  
partial.

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 3, 2006, 10:12pm UTC](https://www.ruby-forum.com/t/text-field-doesnt-call-overridden-activerecord-getters/96968/3 "2006-07-03T22:12:51Z")

</div>

On 7/3/06, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> >

The ActiveRecord form helpers call the before\_type\_cast versions of  
the accessor methods.  
If your column name is example\_field:  
text\_field :target\_list, :example\_field calls  
“example\_field\_before\_type\_cast” on your model instance.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 3, 2006, 10:59pm UTC](https://www.ruby-forum.com/t/text-field-doesnt-call-overridden-activerecord-getters/96968/4 "2006-07-03T22:59:14Z")

</div>

On 7/3/06, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> create a separate method to decorate your model attribute get methods  
> because of this behavior. Is that correct?

It depends. If you have a string column, you can just alias the two  
methods together.  
If you have something more complex, you may need to implement “thing”  
and “thing\_before\_type\_cast” separately.  
Alternately, you could make your own form helper that called your  
normal attribute by name.

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 3, 2006, 10:17pm UTC](https://www.ruby-forum.com/t/text-field-doesnt-call-overridden-activerecord-getters/96968/5 "2006-07-03T22:17:41Z")

</div>

Wilson B. wrote:

> On 7/3/06, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > >
> 
> The ActiveRecord form helpers call the before\_type\_cast versions of  
> the accessor methods.  
> If your column name is example\_field:  
> text\_field :target\_list, :example\_field calls  
> “example\_field\_before\_type\_cast” on your model instance.

So then, in general when using ActiveRecord form helpers, you must  
create a separate method to decorate your model attribute get methods  
because of this behavior. Is that correct?

Wes
