I would like to implement warning errors on model validation with Ruby mongoid gem.
I kinda followed this example found here Efficient way to report record validation warnings as well as errors?
class MyModel
validate :check_warnings
def warnings
@warnings ||= ActiveModel::Errors.new(self)
end
def check_warnings
warnings.add(:some_field, :not_exist) if some_field.blank?
warnings.add(:another_field, :not_exist) if another_field.blank?
end
end
When warnings value is acessed in Rails helper to display warnings, it is empty.
p doc.warnings.size => 0
But if I force a validation error
def check_warnings
warnings.add(:some_field, :not_exist) if some_field.blank?
warnings.add(:another_field, :not_exist) if another_field.blank?
errors.add(:yet_another_field, :not_exist) if yet_another_field.blank?
end
# in helper
p doc.errors.size => 1
p doc.warnings.size => 2
warning errors are preserved.
Is this maybe a mongoid bug or am I missing something.
by
TheR