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

**URL:** <https://www.ruby-forum.com/t/underscore-in-a-models-method-causes-it-not-to-be-called-from-the-form-helper/180983>\
**Category:** Rails\
**Created:** [January 3, 2010, 12:14am UTC](https://www.ruby-forum.com/t/underscore-in-a-models-method-causes-it-not-to-be-called-from-the-form-helper/180983 "2010-01-03T00:14:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 3, 2010, 12:14am UTC](https://www.ruby-forum.com/t/underscore-in-a-models-method-causes-it-not-to-be-called-from-the-form-helper/180983/1 "2010-01-03T00:14:51Z")

</div>

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 removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 3, 2010, 12:22am UTC](https://www.ruby-forum.com/t/underscore-in-a-models-method-causes-it-not-to-be-called-from-the-form-helper/180983/2 "2010-01-03T00:22:29Z")

</div>

On Jan 2, 9:50 pm, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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 removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [January 3, 2010, 12:27pm UTC](https://www.ruby-forum.com/t/underscore-in-a-models-method-causes-it-not-to-be-called-from-the-form-helper/180983/3 "2010-01-03T12:27:40Z")

</div>

Hi,

On 3 Jan., 00:21, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).
