Am I correct in thinking that overriding a default accessor on an ActiveRecord model object is an incomplete way of ensuring that a value is properly munged before the record is saved? For instance if I were to instantiate the object by passing in the @param hash, wouldn''t I bypass the accessor? So is using a before_validation callback the proper way to munge incoming data before the record is validated and saved? John-Mason
Quoting "John-Mason P. Shackelford" <john-mason-Grinw8Jr/8zZYwk40xCobA@public.gmane.org>:> Am I correct in thinking that overriding a default accessor on an > ActiveRecord model object is an incomplete way of ensuring that a value > is properly munged before the record is saved?No, this is the recommended way to do it. Passing in attributes using #attributes= or Klass.new/create calls the accessor. This is probably preferable to a before_validation callback. Tim. -- Tim Bates tim-kZbwfhiKUx26c6uEtOJ/EA@public.gmane.org
> So is using a before_validation callback the proper way to munge > incoming data before the record is validated and saved?Yes. That''s their reason for being. Callback away! ;) -- David Heinemeier Hansson, http://www.basecamphq.com/ -- Web-based Project Management http://www.rubyonrails.org/ -- Web-application framework for Ruby http://macromates.com/ -- TextMate: Code and markup editor (OS X) http://www.loudthinking.com/ -- Broadcasting Brain
Hmm. David writes:> Yes. That''s their reason for being. Callback away! ;)Tim writes:> No, this is the recommended way to do it.So who is right? :) John-Mason
Quoting "John-Mason P. Shackelford" <john-mason-Grinw8Jr/8zZYwk40xCobA@public.gmane.org>:> So who is right? :)Almost assuredly David, but I''m permitted to hold my own opinion. ;) Both techniques will work, and my comment about #create and #attributes= calling the accessors you define is definitely correct. Tim. -- Tim Bates tim-kZbwfhiKUx26c6uEtOJ/EA@public.gmane.org
Tim,> Both techniques will work, and my comment about #create and #attributes= calling > the accessors you define is definitely correct.Good. That approach has the advantage of taking effect immediately rather waiting for a save to trigger the callback. John-Mason