This is probably a Ruby questions but since my issue exists in a Rails
validation I thought I’d post here. I have the following validator
validates_each :thumbnail, :regular do |record, attribute, value|
unless value.nil? &&
value.valid?
record.errors.add(
attribute, value.errors.full_messages[0])
end
end
I’m getting a ‘nil object when you didn’t expect it error’ because the
condition is evaluating value.valid?. I was under the impression that if
the first condition (value.nil?) returned True then the second condition
would not be evaluated.
Does anyone know what I’m missing here?
If I rewrite it as:
validates_each :thumbnail, :regular do |record, attribute, value|
unless value.nil?
record.errors.add(
attribute, value.errors.full_messages[0]) unless value.valid?
end
end
it works fine.
Thanks for any education,
William