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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:04:20+00:00 2026-05-27T18:04:20+00:00

I want to change the table name of the ActiveRecord model class during request

  • 0

I want to change the table name of the ActiveRecord model class during request dynamically.


For example, there are many tables having similar structure(columns):

mydb:
  sample_data_12222
  sample_data_12223
  sample_data_12224
  sample_data_12225
  ...

So, what I want to do is…

_1. Defining the base model class like:

class SampleData < ActiveRecord::Base

_2. Changing the target table during request like:

def action_method
  SampleData.set_table_name "sample_data_#{params[:id]}"
  @rows = SampleData.all

It seems that above code is right if it’s run on non-threaded environment (like on Passenger/mod_rails).
But it’s not thread-safe, so it may not work on threaded-environment (like on JRuby-Rack).

Also I tried creating the delived class like this:

def action_method
  @model_class = Class.new(SampleData)
  @model_class.set_table_name "sample_data_#{params[:id]}"
  @rows = @model_class.all

But it cause memory leaks, though the delived model class is not used any more after request was completed. 🙁


Is there a better way to do that?

  • 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-27T18:04:20+00:00Added an answer on May 27, 2026 at 6:04 pm

    I would use class variables:

    class SampleData < ActiveRecord::Base
      class << self
        @@model_class = {}
    
        # Create derived class for id
        @@model_class[id] = Class.new(SampleData)
        @@model_class[id].set_table_name "sample_data_#{id}"
        # Repeat for other IDs
      end
    end
    

    Now you can use the derived classes over and over, without causing memory leaks.

    Depending on your actual situation (eg you do not know the IDs on beforehand), you could check whether the id is already present in the Hash dynamically and add it if not.

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

Sidebar

Related Questions

I'm having this table class CreateEvents < ActiveRecord::Migration def self.up create_table :events do |t|
I've got the following User model: class User < ActiveRecord::Base # Users table has
I want to change the background color of the table cell when radio button
I have a table where i want to change cell background on mouse over
Hey all, I want to change a value in a table (in a mysql
I have created a table in my application, I want to select (change background
I use several referenced tables with integer primary keys. Now I want to change
how do I change my activerecord model default behavior for the find method? For
Suppose we have the Lyric model: class Lyric < ActiveRecord::Base belongs_to :song end and
I wish to be able to change the table a class is mapped to

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.