In my Rails project I created service object which get params and save Model. I want to handle exception there when e.g. one of the params is not valid. My service object looks like that:
class ModelSaver
include ActiveModel::Validations
attr_reader :model
def initialize(params)
@params = params
end
def errors
@model.errors
end
def save_model
@model ||= Model.new(params)
@model.name = nil
@model.save!
rescue ActiveRecord::RecordNotSaved, ActiveRecord::RecordInvalid => error
@model.errors.add(:base, error.to_s)
false
end
private
attr_reader :params
end
In Model name can't be nil, so when in example I try to @model.name = nil
then service object go to rescure and exit from save_model. Can I somehow continue and add next errors from @model.save!
(if there any) after exception from @model.name = nil
?