I am noticing that text_field returns empty strings instead of nil when inserting new records. Am I missing something? Does anyone have a clean way of insuring that empty fields are inserted as NULL in the database? -- Lon Baker Speedymac LLC http://www.speedymac.com AIM: spdemac Skype: spdemac
Lon Baker wrote:> Does anyone have a clean way of insuring that empty fields are inserted > as NULL in the database?Here''s what I do (although I''m sure this isn''t the Ruby-est solution). This will trim whitespace and assign to nil if the result is an empty string: class Article < ActiveRecord::Base before_save :nilify protected def nilify self.title = nil if self.title and (self.title = self.title.strip).empty? end end -- Steve