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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:28:36+00:00 2026-05-18T01:28:36+00:00

I have a complex save process (a cyclical reference that needs to be resolved

  • 0

I have a complex save process (a cyclical reference that needs to be resolved by validating and saving and one model, validating and saving another and then updating the first with an id from the second) that has rollbacks depending on if something bad happened etc.

My models are related in this way:

  • Accounts have one SiteContact.
  • Accounts have many Customers.
  • SiteContacts are a type of Customer.
  • Customers belong to an Account

Because of the cyclical reference (i.e. Customers are invalid WITHOUT an Account but an Account needs a SiteContact which is a type of Customer.) I decide to save the Account with a blank id for site contact first, then save the site_contact.

I have code similar to this:

# remove the site contact from the account because its not valid yet.
site_contact = params[:account].delete(:site_contact_attributes)
account = Account.new(params[:account])

# some pseudo code
# start database transaction
# validate account
# if valid then
#   save account
#   site_contact.account_id = account.id
#   validate site_contact
#   if valid then
#     account.site_contact_id = site_contact.id
#     save account
#     break out!
#
#   else
#     rollback transaction
#   end
# else
#   rollback transaction
# end

if Account.exists?(account)
  # everything was good! flash[:notice] and redirect somewhere
else
  # something bad happened, go back to the page and display the errors.
end

Is it possible to override the Account#save or some other method and move all of that code into my controller? I was looking at before_save and before_create but I’m worried there might be some unintended recursion since I have to save the account twice.

Any ideas?

  • 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-18T01:28:37+00:00Added an answer on May 18, 2026 at 1:28 am

    In your controller:

    site_contact = SiteContact.new(params[:account].delete(:site_contact_attributes))
    account = Account.new(params[:account])
    
    if account.save_nested(site_contact)
     # success
    else
     # error
    end
    

    In your Account model.

    class Account < ActiveRecord::Base
      def save_nested(site_contact)
        Account.transaction do
          save!
          site_contact.account_id = self.id
          site_contact.save!
          self.site_contact_id = site_contact.id
          save!
          return true
        end
        return false
      end
    end
    

    The save! will throw an exception when the validation fails. This will automatically roll back the transaction.

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

Sidebar

Related Questions

I have a 'complex item' that is in XML, Then a 'workitem' (in xml)
I have a complex .NET Remoting server app that provides a couple of services.
I have a complex command that I'd like to make a shell/bash script of.
I have some complex stored procedures that may return many thousands of rows, and
Assuming Visual C/C++ 6, I have a complex data structure of 22399 elements that
I have a complex query with group by and order by clause and I
I have a complex sharepoint deploy with multiple EventReceivers and Workflows. I also have
I have two complex (i.e. objects with string, int, double, List and other home
I have a complex WPF UserControl made of other ContentControl templates which contain sets
If i have a complex object, what is the best practice pattern to write

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.