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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:04:06+00:00 2026-05-16T23:04:06+00:00

Also known as the <<User has many Databases question.>> The environment My app is

  • 0

Also known as the <<“User has many Databases” question.>>

The environment

My app is modeled like so:

user has_many databases  
database has_many tables  
table has_many rows  
row habtm(+value) columns   

you get the idea!

So instead of modelling a database inside a database,
I would like to have:

  • a sqlite3 database that holds the users and
  • many sqlite databases for each user

Each user will LCRUD his tables in his databases (similar to phpmyadmin)

The problem

I would like to have thread safe per-request configuration for
database connection and table_name

class Table < ActiveRecord::Base
end

# in some controller
# set the connection to the user-selected database from some DB list
Table.connection = current_user.session.connection
# set the name to the user-selected table from some tables list
Table.table_name = params[:table_name]
@rows = Table.all #display them

EDIT
As you see, the connection is global and shared between threads, but as per my app’s specs, each user has it’s own connection. Now imagine that 2 different users make 2 requests at the same time.

The options?

  • I give up ActiveRecord and use bare-bones DB driver
  • I give up thread saftey
  • 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-16T23:04:07+00:00Added an answer on May 16, 2026 at 11:04 pm

    I believe this is the incantation:
    Use Class.new(AR::Base) to dynamically create classes

    post_class = Class.new(ActiveRecord::Base)
    post_class.connection = set_up_connection()
    post_class.table_name = :posts
    
    @posts = post_class.all
    puts @posts
    
    # note: post_class will get GC'ed at scope end just like any var, sweet!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.