Wes Gamble
2006-Jul-03 19:27 UTC
[Rails] 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 -- Posted via http://www.ruby-forum.com/.
Wes Gamble
2006-Jul-03 19:50 UTC
[Rails] Re: text_field doesn''t call overridden ActiveRecord getters
Wes Gamble 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? > > WesI 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 -- Posted via http://www.ruby-forum.com/.
Wilson Bilkovich
2006-Jul-03 20:11 UTC
[Rails] Re: text_field doesn''t call overridden ActiveRecord getters
On 7/3/06, Wes Gamble <weyus@att.net> wrote:> Wes Gamble wrote: > > All, > > > > In a template, I have > > > > <%= text_field :target_list, :DateReceived, > > { :title => ''uploaded_at'', > > :class => > > ''target_list_info'', > > :disabled => ''true'' } %> > >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.
Wes Gamble
2006-Jul-03 20:17 UTC
[Rails] Re: Re: text_field doesn''t call overridden ActiveRecord gett
Wilson Bilkovich wrote:> On 7/3/06, Wes Gamble <weyus@att.net> 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 -- Posted via http://www.ruby-forum.com/.
Wilson Bilkovich
2006-Jul-03 20:58 UTC
[Rails] Re: Re: text_field doesn''t call overridden ActiveRecord gett
On 7/3/06, Wes Gamble <weyus@att.net> wrote:> Wilson Bilkovich wrote: > > On 7/3/06, Wes Gamble <weyus@att.net> 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? >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.