On Sunday, December 2, 2018 at 2:55:00 PM UTC-8, Steven Garcia wrote: > > Coming from ActiveRecord I was a bit perplexed by Sequel's behavior when > validations fail. > > In a Rails app I could simply run a condition on @model.valid? and keep it > moving. > > But Sequel actually throws an error, which breaks the app - so I find > myself doing this: > > begin @site.update(r.params[:site]) > flash.now[:done] = 'Update complete' > rescue Sequel::ValidationFailed > flash.now[:fail] = 'Woops! Something went wrong.' > end > > Which 100% works, but feels a bit clunky to me. >
I usually abstract this to a method called handle_validation_failure(template_to_render, flash_error_message). Sequel is designed to raise exceptions for save failures by default, as it makes it so you must explicitly handle failures. By just returning false for save failures (ActiveRecord's behavior), it's fairly easy to silently ignore a failure. As you've seen, you can get that behavior if you want it. In apps I've converted from ActiveRecord to Sequel, I've used raise_on_save_failure = false temporarily for an easier conversion, then turned it back on, and often found cases where the code wasn't actually handling the failure and was treating the failure like success. 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.
