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 1078065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:40:11+00:00 2026-05-16T21:40:11+00:00

I have a special case Model which must not become part of an outer

  • 0

I have a special case Model which must not become part of an outer transaction:

Outer.Transaction do
  ...

  Inner.create(:blah)

  ...
end

How do I stop Inner becoming part of the transaction, assuming Inner should know nothing about which specific transaction it is getting pulled into?

For example, creating an inner transaction is a no go, because that too will become part of the outer transaction.

I want to do this because the inner model needs to write immediately and not wait for the outer transaction to commit.

  • 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-05-16T21:40:12+00:00Added an answer on May 16, 2026 at 9:40 pm

    I’m curious as to what would require such a construct!

    I think you’re going to struggle to do it without a bit of hackery as you’ve described. For example, you could, in mysql, set the table storage type for Inner to one that doesn’t support transactions (MyIsam eg) whilst keeping other classes’ tables storage with something that does support transactions (YUK!).

    If you can, you’d almost certainly be better off delaying the Inner.create until after the transaction. You can use begin with ensure to make sure that the create always happens. Something like:

    create_inner = false
    begin
      Outer.transaction.do
        ...
        create_inner = true # instead of Inner.create(:blah)
        ...
      end
    ensure
      if create_inner
        Inner.create(:blah)
      end
    end
    

    This would become more complicated if the rest of your block depends on the created Inner instance. You could probably create the instance in the block and set created_inner to false at the end of the block so that, if the code runs without exception it will have been created in the transaction and you won’t create again in the ensure.

    If you want to do it in the general case you could define a class method on Inner to execute a block but always create an Inner object. You’d need to add an after_create to Inner too. You would rely on the Inner.create call in the block to create it when the transaction succeeds but if it is rolled back then you’d need to create it afterwards. For example:

    class Inner < ActiveRecord::Base
    
      def self.ensure_created(&block)
        Thread.current[:created_inner] = false        
        begin
          block.call
        rescue => e
          if Thread.current[:created_inner]
            Inner.create(:blah)
          end
          raise e
        end
      end
    
      def after_create
        # Flag that an instance has been created in this thread so
        # that if we rollback out of a transaction we can create again
        Thread.current[:created_inner] = true
      end
    

    You’d then call it like:

    Inner.ensure_created do
      Outer.transaction do
        ...
        Inner.create(:blah)
        ...
      end
    end
    

    HOWEVER, there’s plenty of downsides to this approach and I’m not sure I’d advocate it. It is complicated. It won’t work if ActiveRecord::Rollback is raised as that exception won’t bubble out of the Outer.transaction but will cause the Inner instance not to be created. It won’t work properly when two or more calls are nested. And finally I haven’t tested it thoroughly – use with caution!

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

Sidebar

Related Questions

I have a call to a model which is just a standard class (not
I'd like to capitalize the first letter of a string which could have special
There are some products for which I would like to have a special checkout
I have a project which does not follow the rails nameing conventions, because it
I have a Post model and a View model (which logs the view of
I have a special case, for example in table ta in database A ,
I have a special case where I would need the ability to use multiple
Is it possible to have special characters like åäö in the key? If i
I'm trying to query by iteration but our iteration names have special characters (e.g.
Ok, I understand that using strings that have special characters is an encoding issue.

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.