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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:03:46+00:00 2026-05-24T08:03:46+00:00

I want to destroy all the questions to do with a particular user. Some

  • 0

I want to destroy all the questions to do with a particular user. Some questions are protected though and may prevent themselves from being destroyed and raise an exception.

I would expect the following code to destroy either all questions or none however it doesn’t – if there is a protected question amongst the others it doesn’t roll back the previous destroy actions – why is this?

class User < ActiveRecord::Base
 ...

  Questions.transaction do
    # protected questions will raise a runtime exception
    Questions.destroy_all(:user_id => self.id)
  end
end
  • 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-24T08:03:47+00:00Added an answer on May 24, 2026 at 8:03 am

    Grrr, just realised I’d hit this problem before and wasted a load of time then before figuring it out.

    The issue is that the test is being done in RSpec which itself uses transactions and removes transactional functionality from the code as a result (n.b. anyone reading this from the RSpec team – it would be great to have a warning when parsing code that contains transactions – ty!).

    In order to make the transaction work within RSpec wrap it in the following code:

    describe "the set of cases you want to address" do
    
      # make sure this next line is contained within a describe block or it'll affect everything
      self.use_transactional_fixtures = false
    
      after(:each) do
        # destroy all objects you created (since RSpec won't roll them back for you)
        # use delete rather than destroy to force removal
        User.delete_all
        Question.delete_all
      end
    
      it "should not destroy any questions when one fails to be destroyed" do
        # assuming one of the questions throws an error on being destroyed
        expect{
          @user.destroy
        }.to change{ Question.all.count }.by(0)
      end
    end
    

    end

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

Sidebar

Related Questions

I want to destroy all i-frames of a video. Doing this I want to
I want to create and then destroy buttons, all with code, but can't find
I'm trying to cache user controls and on some pages i want to cache
I'm trying to nest some routes under the namespace, account. I want user management
I have a vector (pflist) of Pictureframes, and I want to destroy all of
I want destroy all sessions at one time. I have tried Session.Abondon() method but
When Gary Bernhardt executes a shell command from Destroy All Software , for example
I want to explicitly destroy an object (call the destructor on it and all
I want to use the Assert keyword in my android apps to destroy my
We need to create & destroy instances of QApplication, as we want to use

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.