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

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

Hi,

> 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 ?

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