Thanks. It's bad example, actually I'm working with custom forms (nesting
available).
class EntityForm
def execute
result = nil
DB.transaction do
DB.after_rollback { result = validation_errors }
raise Sequel::Rollback unless self.valid?
result = entity.save
end
result
end
def valid?
nested_forms.each(&:valid?)
custom_validation
end
end
On Tuesday, August 7, 2018 at 11:27:54 PM UTC+3, Jeremy Evans wrote:
>
> On Tuesday, August 7, 2018 at 12:15:22 PM UTC-7, Alexander Popov wrote:
>>
>> OK.
>>
>> So, if I want to:
>>
>> - Get result of transaction if everything is OK
>>
>>
>> - Get result of `after_rollback` block if there is `Sequel::Rollback`
>>
>>
>> - And get raised any other exception
>>
>> I should use local variables like:
>>
>> def foo
>> result = nil
>> DB.transaction do
>> DB.after_rollback { result = validation_errors }
>> raise Sequel::Rollback unless entity.valid?
>> result = entity.save
>> end
>> result
>> end
>>
>> Right? There is no more compact way?
>>
>
> That should work if you want to return the entity if the transaction
> commits, the validation errors if it is not, and any exceptions to be
> raised.
>
> Your particular example could be made simpler, relying on the fact that
> Sequel::Model uses transactions when saving:
>
> def foo
> entity.save(raise_on_failure: false) || validation_errors
> end
>
> However, if you are doing other things in your transaction block, you may
> need your more elaborate construction. Note that in your code, validations
> are run twice (once in valid? and once in save).
>
> Thanks,
> Jeremy
>
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.