Tony Mobily
2006-Jul-06 04:39 UTC
[Rails] Not accepting nil values in fields when CREATING a record
Hello, I have a question for you. In my application, I want to make sure that the database doesn''t have any NULLs in fields. I can''t use validates_presence_of, because it will puke if there''s an empty string ("") So... is there an EASY way to check if a list of fields is null? Right now, I''m doing: def validate() [...] errors.add(:mailing_middle_name, "cannot be nil") if mailing_middle_name.nil? errors.add(:company, "cannot be nil") if company.nil? errors.add(:address_line_2, "cannot be nil") if address_line_2.nil? [...] end But it''s ugly. Plus, I realise that passing "nil" is a nice way NOT to specify every single value when you update a record. So... to expand my previous question, is there a way to prevent nil values when an object is CREATED, but then allow them (which measn "don''t touch whatever is there") when it''s updated? Am I missing something very obvious? :-D Merc. -- Posted via http://www.ruby-forum.com/.
Socheat Yi
2006-Jul-06 13:34 UTC
[Rails] Re: Not accepting nil values in fields when CREATING a recor
I think the better way, you can use condition to check for nil fields.. For example: field1 = field1==nil?'''':field1 in this case, if it is nil, field1 will get empty string instead. Socheat Tony Mobily wrote:> Hello, > > I have a question for you. > In my application, I want to make sure that the database doesn''t have > any NULLs in fields. > I can''t use validates_presence_of, because it will puke if there''s an > empty string ("") > > So... is there an EASY way to check if a list of fields is null? > Right now, I''m doing: > > def validate() > [...] > errors.add(:mailing_middle_name, "cannot be nil") if > mailing_middle_name.nil? > errors.add(:company, "cannot be nil") if company.nil? > errors.add(:address_line_2, "cannot be nil") if > address_line_2.nil? > [...] > end > > But it''s ugly. > Plus, I realise that passing "nil" is a nice way NOT to specify every > single value when you update a record. > > So... to expand my previous question, is there a way to prevent nil > values when an object is CREATED, but then allow them (which measn > "don''t touch whatever is there") when it''s updated? > > Am I missing something very obvious? :-D > > Merc.-- Posted via http://www.ruby-forum.com/.
Inge Jørgensen
2006-Jul-06 14:13 UTC
[Rails] Re: Not accepting nil values in fields when CREATING a recor
i think this will work too: field1 = field1 || '''' Socheat Yi wrote:> I think the better way, you can use condition to check for nil fields.. > For example: field1 = field1==nil?'''':field1 > in this case, if it is nil, field1 will get empty string instead. > > Socheat > > > > Tony Mobily wrote: > >> Hello, >> >> I have a question for you. >> In my application, I want to make sure that the database doesn''t have >> any NULLs in fields. >> I can''t use validates_presence_of, because it will puke if there''s an >> empty string ("") >> >> So... is there an EASY way to check if a list of fields is null? >> Right now, I''m doing: >> >> def validate() >> [...] >> errors.add(:mailing_middle_name, "cannot be nil") if >> mailing_middle_name.nil? >> errors.add(:company, "cannot be nil") if company.nil? >> errors.add(:address_line_2, "cannot be nil") if >> address_line_2.nil? >> [...] >> end >> >> But it''s ugly. >> Plus, I realise that passing "nil" is a nice way NOT to specify every >> single value when you update a record. >> >> So... to expand my previous question, is there a way to prevent nil >> values when an object is CREATED, but then allow them (which measn >> "don''t touch whatever is there") when it''s updated? >> >> Am I missing something very obvious? :-D >> >> Merc. >> > > >