Text_field doesn't call overridden ActiveRecord getters

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

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

On 7/3/06, Wes G. [email protected] 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.

On 7/3/06, Wes G. [email protected] 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.

Wilson B. wrote:

On 7/3/06, Wes G. [email protected] 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