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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:36:17+00:00 2026-05-25T18:36:17+00:00

I have a somewhat special use case, where I’d like to create a method

  • 0

I have a somewhat special use case, where I’d like to create a method that accepts a block, such that anything that happens inside that block is not written to the DB.

The obvious answer is to use transactions like so:

def no_db
  ActiveRecord::Base.transaction do
    yield  
    raise ActiveRecord::Rollback 
  end
end

But the trouble is that if my no_db method is used inside of another transaction block, then I’ll ned up in the case of nested transactions. The drawback here is that nested transactions are only supported by MySQL, and I need support for PG, but more importantly SQLite (for tests). (I understand that PG is supported via savepoints, how reliable is that? performance hit?).

The other problem with this type of approach is that it seems really inefficient, writing things to a DB, and then rolling them back. It would be better if I could do something like this:

def no_db_2
  # ActiveRecord::Base.turn_off_database
    yield
  # ActiveRecord::Base.turn_on_database
end

Is there such a method? Or a similar approach to what I’m looking for? I think it needs to be fairly low level..

(Rails version is 3.0.5, but I would be happy if there were an elegant solution for Rails 3.1)

  • 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-25T18:36:17+00:00Added an answer on May 25, 2026 at 6:36 pm

    This might be one way to do it:

    class Book < ActiveRecord::Base
      # the usual stuff
    end 
    
    # Seems like a hack but you'll get the
    # transaction behavior this way...
    class ReadOnly < ActiveRecord::Base
      establish_connection "#{Rails.env}_readonly"
    end
    

    I would think that this…

    ReadOnly.transaction do
      Book.delete_all
    end
    

    …should fail.

    Finally, add another connection to config/database.yml

    development:
      username: fullaccess
    
    development_readonly:
      username: readonly
    

    One downside is the lack of support for a read-only mode in the sqlite3-ruby driver. You’ll notice that the mode parameter doesn’t do anything yet according to the documentation. http://sqlite-ruby.rubyforge.org/classes/SQLite/Database.html#M000071

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

Sidebar

Related Questions

I have somewhat unconventional use of exceptions in my app. If an exception happens
I have several somewhat separate programs, that conceptually can fit into a single project.
I have somewhat of a problem. We have a centralized interface engine that will
I heard that Xtext ultimately uses ANTLR but their grammar specification files have somewhat
I have some tests that use guids. The guids used don't need to be
I have a Python 2.6 script that is gagging on special characters, encoded in
Does any one have a handy ready to use method to escape wildcards, invalid
I have somewhat deep Json structure like below incoming over the wire into C#
I'd like to have my application run in somewhat of a stealth mode. Two
I do have somewhat of a feeling that people will try to beat me

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.