Text_field and overridden methods

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

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

Hello,

On Jul 30, 10:54 pm, jemminger [email protected] 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