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? :smiley:

Merc.

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 M. 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? :smiley:

Merc.

i think this will work too:

field1 = field1 || ‘’

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs