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

  • Home
  • SEARCH
  • 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 675419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:48:58+00:00 2026-05-14T00:48:58+00:00

I have two models: Person and Address which I’d like to create in a

  • 0

I have two models: Person and Address which I’d like to create in a transaction. That is, I want to try to create the Person and, if that succeeds, create the related Address. I would like to use save semantics (return true or false) rather than save! semantics (raise an ActiveRecord::StatementInvalid or not).

This doesn’t work because the user.save doesn’t trigger a rollback on the transaction:

class Person
  def save_with_address(address_options = {})
    transaction do
      self.save
      address = Address.build(address_options)
      address.person = self
      address.save
    end
  end
end

(Changing the self.save call to an if self.save block around the rest doesn’t help, because the Person save still succeeds even when the Address one fails.)

And this doesn’t work because it raises the ActiveRecord::StatementInvalid exception out of the transaction block without triggering an ActiveRecord::Rollback:

class Person
  def save_with_address(address_options = {})
    transaction do
      save!
      address = Address.build(address_options)
      address.person = self
      address.save!
    end
  end
end

The Rails documentation specifically warns against catching the ActiveRecord::StatementInvalid inside the transaction block.

I guess my first question is: why isn’t this transaction block… transacting on both saves?

  • 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-14T00:48:58+00:00Added an answer on May 14, 2026 at 12:48 am

    How about this?

    class Person
      def save_with_address(address_options = {})
        tx_error = false
        transaction do
          begin
            self.save!
            address = Address.build(address_options)
            address.person = self
            address.save!
          rescue Exception => e
            # add relevant error message to self using errors.add_to_base
            raise ActiveRecord::Rollback 
            tx_error = true 
          end
        end
        return true unless tx_error
    
        # now roll back the Person TX.
        raise ActiveRecord::Rollback
        return false
      end
    end
    

    I don’t like the way the TX is implemented. But this is how you can work around the issues.

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

Sidebar

Related Questions

I have two models in my application, Transaction and Person, with a many-to-many relationship.
I have a Rails simple application that has two main models. A person model
I have two models: Person and Relation. The second one stores information about relations
I have two models, Article and Post that both inherit from a base model
I have two models, Room and Image . Image is a generic model that
I have two models, A and B, and one light, L. I would like
For example I have two model objects, Person and Address. Address has a reference
I have the following two models class ContactField < ActiveRecord::Base end class Address <
I have two models in Django linked together by ManyToMany relation like this: class
As an example, I have two models: Household and Person : from django.db import

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.