I have a model where initially, they we don''t insert an gov_id value because it isn''t known. The field is given a value of "" because it is on the form. I then added code to the ''create'' method to set that value to nil if it is blank which seems to be fine but then it fails this validation (if NULL) validates_length_of :gov_id, :maximum => 18, :message => "Maximum 18 characters and numbers combined" seems like I can''t win here...because I can''t pass validation uniqueness if it is empty ("") and I can''t pass validation length if it is NULL Suggestions? Craig
Ray Baxter
2006-Mar-10 20:26 UTC
[Rails] Re: validation null objects and objects with data
Craig White wrote:> I have a model where initially, they we don''t insert an gov_id value > because it isn''t known. > > The field is given a value of "" because it is on the form. > > I then added code to the ''create'' method to set that value to nil if it > is blank which seems to be fine but then it fails this validation (if > NULL) > > validates_length_of :gov_id, :maximum => 18, :message => "Maximum > 18 characters and numbers combined" > > seems like I can''t win here...because I can''t pass validation uniqueness > if it is empty ("") and I can''t pass validation length if it is NULLTry validates_length_of :allow_nil ... Ray
Craig White
2006-Mar-11 00:29 UTC
[Rails] Re: validation null objects and objects with data
On Fri, 2006-03-10 at 12:25 -0800, Ray Baxter wrote:> Craig White wrote: > > I have a model where initially, they we don''t insert an gov_id value > > because it isn''t known. > > > > The field is given a value of "" because it is on the form. > > > > I then added code to the ''create'' method to set that value to nil if it > > is blank which seems to be fine but then it fails this validation (if > > NULL) > > > > validates_length_of :gov_id, :maximum => 18, :message => "Maximum > > 18 characters and numbers combined" > > > > seems like I can''t win here...because I can''t pass validation uniqueness > > if it is empty ("") and I can''t pass validation length if it is NULL > > Try validates_length_of :allow_nil ... >---- that didn''t work but :allow_nil => true did work That wasn''t offered in AWDWR book but is in the rails.api thanks Craig