# Text\_field and overridden methods

**URL:** <https://www.ruby-forum.com/t/text-field-and-overridden-methods/110749>\
**Category:** Rails\
**Created:** [July 30, 2007, 4:41pm UTC](https://www.ruby-forum.com/t/text-field-and-overridden-methods/110749 "2007-07-30T16:41:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Veerman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@Mark\_Veerman](https://www.ruby-forum.com/u/Mark_Veerman)\
**Post date:** [July 30, 2007, 4:41pm UTC](https://www.ruby-forum.com/t/text-field-and-overridden-methods/110749/1 "2007-07-30T16:41:56Z")

</div>

Hello,

I’ve got a model with an attribute that I’ve overridden – or at least  
defined an implementation for. It seems that when I call  
form.text\_field :foo, it’s grabbing foo out of the attributes hash in  
the object rather than calling the method. I can get around this by  
using text\_field\_tag :foo, @object.foo.

Is that cool, or is there a better way? It seems interesting that  
text\_field would grab stuff directly out of the attributes hash,  
thereby assuming my implementation. If they called the method ‘foo’ it  
would preserve my implementation. Then, if there wasn’t an  
implementation, ActiveRecord would step in and handle things.

Thanks!  
Mark

---

<div class="post-metadata">

**Author:** ![Mark\_Veerman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@Mark\_Veerman](https://www.ruby-forum.com/u/Mark_Veerman)\
**Post date:** [July 31, 2007, 4:55am UTC](https://www.ruby-forum.com/t/text-field-and-overridden-methods/110749/2 "2007-07-31T04:55:25Z")

</div>

what does your implementation look like?

something like this ought to work as expected:

class Foo  
def foo  
“custom value of #{self[:foo]}”  
end  
end

---

<div class="post-metadata">

**Author:** ![Mark\_Veerman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f475e1/32.png) [@Mark\_Veerman](https://www.ruby-forum.com/u/Mark_Veerman)\
**Post date:** [August 1, 2007, 10:15pm UTC](https://www.ruby-forum.com/t/text-field-and-overridden-methods/110749/3 "2007-08-01T22:15:24Z")

</div>

Hello,

On Jul 30, 10:54 pm, jemminger [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> what does your implementation look like?
> 
> something like this ought to work as expected:
> 
> class Foo  
> def foo  
> “custom value of #{self[:foo]}”  
> end  
> end

Thanks for the response. That’s exactly what I was doing. I have a  
plugin that defines an implementation for foo like you describe.

We did figure out what was going on though. For each model attribute  
in Rails there’s another method called value\_before\_type\_cast. In the  
case of your example: the attribute foo would have two Rails  
‘MethodMissing’ getters: foo, and foo\_before\_type\_cast. It seems that  
the text\_field API uses the latter, so my implementation for foo was  
never called. We solved the problem by implementing  
foo\_before\_type\_cast and having it call through to foo. I’m not sure  
if that’s the best way, but it works and makes sense.

I don’t really know why there is that other before\_type\_cast method,  
or why text\_field seems to prefer it.

Thanks,  
Mark
