On Sep 21, 2011, at 2:45 PM, Banjoey wrote: > Well I guess you think you're just sooooo smart. Well, you are... > > That got it. > > Is there a way for me to keep the form fields so the user could change > the values and then only overwrite if the value was not blank? Would > that require me to overwrite the logic in the controller completely, > or is there an easy way to override that behavior? >
(quick, before I forget the details) Here's a rough outline of what's going on (I'm assuming you're using a standard Hobo create action): - Hobo creates a new object - calls attributes= on it -- Rails loops over each attribute --- for the 'audio' attribute, it calls 'audio=' ---- the assign method of Attachment gets called ----- before_post_processing callbacks fire ----- before_audio_post_processing callbacks fire ----- actual post-processing ----- after_audio_post_processing callbacks fire ----- after_post_processing callbacks fire --- for the description attribute, it calls 'description=' ...etc... - Hobo calls save on the object -- before_validation callbacks fire -- validation -- after_validation callbacks fire -- before_save callbacks fire -- before_create callbacks fire (only for new records, natch) -- record gets saved / created -- after_create callbacks fire (for new records) -- after_save callbacks fire -- after_commit callbacks fire More detail than you probably need, but just in case anybody runs across this in future. Anyways, I'd say the best place to actually do the mp3info stuff would be in before_validation. Your callback would look something like: before_validation :set_fields_from_audio def set_fields_from audio return unless audio? # same code as before, but use self.title = xxx if self.title.blank? end Also note that you may need to catch errors from Mp3Info as this runs BEFORE the content-type checks in validates_attachment_*. With this modification, you can put :required on the fields that can't be blank, and the user will only get an error if they leave the fields blank AND the mp3 doesn't contain the needed data. --Matt Jones -- You received this message because you are subscribed to the Google Groups "Hobo Users" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/hobousers?hl=en.
