wdick01-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2010-Jan-02 21:50 UTC
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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-Jan-02 23:21 UTC
Re: Underscore in a model''s method causes it not to be called from the form helper
On Jan 2, 9:50 pm, "wdic...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org" <wdic...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
wdick01-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2010-Jan-03 11:26 UTC
Re: Underscore in a model''s method causes it not to be called from the form helper
Hi, On 3 Jan., 00:21, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jan 2, 9:50 pm, "wdic...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org" <wdic...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> > 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 ?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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.