Underscore in a model's method causes it not to be called from the form helper

Hi,
I’m using rails 2.3.5 and found following behavior, which I can’t
explain. Maybe someone can.

I have a model named ‘Game’. Its database schema contains a field
named ‘pconfig_src’.
I want to lazy load the pconfig_src value, so added following method
to the model:

def pconfig_src
read_attribute(‘pconfig_src’) || GameHost::phost_pconfig_src
end

GameHost::phost_pconfig_src returns the default value stored in a
file.

Now if I create a form in my view with form_for, like:

<% form_for(@game, :html => { :id => “edit_game” }) do |f| %>

<%= f.text_area :pconfig_src, :cols => 80, :rows => 40 %>

my Game#pconfig_src method will not be called.
But if I change the method’s name to ‘pconfigsrc’ and the symbol’s
name accordingly, then it is called.
Why?

Greetings,
Waldemar

You received this message because you are subscribed to the Google
Groups “Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

On Jan 2, 9:50 pm, “[email protected][email protected]
wrote:

my Game#pconfig_src method will not be called.
But if I change the method’s name to ‘pconfigsrc’ and the symbol’s
name accordingly, then it is called.
Why?

Well when the method name is called pconfigsrc then there’s no clash
with the accessor rails would define normally and so life is simple.
However, overriding the default accessor should work just fine. Could
you post more of the source of the Game class ?

Fred

You received this message because you are subscribed to the Google
Groups “Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

Hi,

On 3 Jan., 00:21, Frederick C. [email protected] wrote:

However, overriding the default accessor should work just fine. Could
you post more of the source of the Game class ?

Ok, I created a small demo and debugged the problem. Not surprisingly,
it works as designed:
The form helper (InstanceTag class) uses the value_before_type_cast
method to get the model field’s raw value. So in my case
‘pconfig_src_before_type_cast’ is called instead of my overwritten
method.

What would be a good way to lazy initialize a field’s default value?

Waldemar

You received this message because you are subscribed to the Google
Groups “Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.