title: Rails saving two different model instances as a singular transaction
body: <p>I am writing a service object for a sign up form that records data for a User and a Company model (FYI, I flat out refuse to use nested_attributes). </p>
  
  <p>A User cannot exist without the presence of a Company (belongs_to).</p>
  
  <p><strong>IF</strong> the company succeeds and the user does not succeed in saving, how do I roll back the creation of the company? </p>
  
  <p>I have tests copied below to demonstrate the point..</p>
  
  <pre><code>  context 'when both are valid?' do
      subject { -&gt; { sign_up_object.save } }
      it { should change(Company, :count).by(1) }
      it { should change(sign_up_object, :company).to be_a Company }
      it { should change(User, :count).by(1) }
      it { should change(sign_up_object, :user).to be_a User }
    end
    context 'when COMPANY is invalid' do
      subject { -&gt; { sign_up_object.save } }
      before { allow_any_instance_of(Company).to receive(:save!).and_return false }
      it { should change(User, :count).by(0) }
      it { should change(Company, :count).by(0) }
    end
    context 'when USER is invalid' do
      before { allow_any_instance_of(User).to receive(:save!).and_return false }
      subject { -&gt; { sign_up_object.save } }
      it { should change(User, :count).by(0) }
      it { should change(Company, :count).by(0) }  -&gt;&gt;&gt;&gt; this one fails!!!
    end
  </code></pre>
  
  <p>The code I have at the moment looks something like this</p>
  
  <pre><code>class SignUp
  
    ......
  
    def save_resources
      ActiveRecord::Base.transaction do
        save_company
        save_user
      end
    end
  
    def save_company
      company = new_company
      self.company = company if company.save!
    end
  
    def save_user
      user = new_user
      self.user = user if user.save!
    end
  end
  </code></pre>
  
  <p>I'm sure the <code>ActiveRecord::Base.transaction</code> block isn't actually doing anything, as my tests show the user spec as the only one as failing, as a company count is going up by 1.</p>