Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8051579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:19:59+00:00 2026-06-05T07:19:59+00:00

Let us say we have the following scenario: class MyModel < ActiveRecord::Base after_save :throw_after_save

  • 0

Let us say we have the following scenario:

class MyModel < ActiveRecord::Base
  after_save :throw_after_save
  after_commit :throw_after_commit

  private
    def throw_after_save
      raise "raising on after_save"
    end

    def throw_after_commit
      raise "raising on after_commit"
    end
end

class MyController < ApplicationController
  def callback
    begin
      MyModel.new(params).save
    rescue
      flash[:alert] = "Failed persisting to external system. Try again."
      Airbrake.notify(
        error_class: "External System Persistence",
        error_message: "External System Persistence: Failed to persist data",
        parameters: params
      )
    end

    redirect_to root_path
  end
end

We get a callback from an external system (where the user fills out some data and creates a temporary set of attributes for an account).

Let us assume that we want to persist some data locally after the callback to us is made. After this data is persisted, we want to make a call to the external system to complete the account creation process. The external system shall return a result informing us of success with some additional data that we need to persist locally. We also know that in some exceptional cases, persistence on the remote system will not occur successfully (let us say system is not available, or something goes wrong on their end).

The objective is to capture external persistence exceptions as well as successes and act accordingly. In the case of successes, everything is hunky dory: the additional data is stored locally, the redirect_to root_path happens. In the case of exceptions, however, we’d like to indicate this to the user (perhaps set a flash[:alert] to be displayed in the view).

We had tried to use ActiveRecord::Callbacks to throw an exception from the model after_save and after_commit and handle that exception at the controller by setting an alert and possibly passing the exception to some exception notification system (like Airbrake). In the case of after_save, the exception is thrown by the model and caught by the controller, but the record is not saved (and we have the requirement of storing the partial data even in the case of exceptions with the external system – this is not acceptable). In the case of after_commit, the exception is not thrown and picked up by the controller, but the partial record is persisted. This means we cannot notify the user of the exception (unless we implement some notification pushing mechanism – which is overkill).

As it turns out, we can set errors on the model on after_save, which is nice. But, is this a good general pattern to handle this sort of scenario?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:20:00+00:00Added an answer on June 5, 2026 at 7:20 am

    You don’t have a lot of options in there. Since the logic itself of what you are asking is complex, I’d move the code to save the record to somewhere else (maybe a class method or another class, representing a service) and when someone wanted to save this specific object it would have to go through this service model.

    For instance:

    class MyModelPersistenceService
    
        def save_model( model )
          result = model.save
          if result
            call_external_service
          end
        end 
    
    end
    

    Services are not that common in Ruby/Rails projects but they are a nice fit for this kind of usage (and abusing AR callbacks usually leads to hard to test objects).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following code: public class Foo { private int x;
Let's say we have following this: <p class=first>This is paragraph 1.</p> <p class=second>This is
Let's say I have the following class hierarchy: TaskViewer inherits from ListViewer<Task> which in
Referring to this question, let's say we have the following scenario - A model
Let's say I have the following scenario: ComObjectClass firstCOMObject = new ComObjectClass(); ComObjectClass secondCOMObject
Let's say I have following ORM classes (fields removed to simplify): class Animal(models.Model): say
Let's say I have following models: class Worker { int Id; string firstname; string
Let's say in some abstract ViewModel base-class I have a plain-old property as follows:
Let's say I have the following scenario: <form action=/something.php method=GET>Click me</div> <script type=text/javascript><!-- $('form').submit(function(e)
Let's say we have following code: struct A{ virtual ~A(){} void f(){ p =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.