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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:43:16+00:00 2026-06-02T16:43:16+00:00

I used ActiveRecord::Base.set_table_name to set my table name on a dynamically created ActiveRecord class.

  • 0

I used ActiveRecord::Base.set_table_name to set my table name on a dynamically created ActiveRecord class. Now I need to know how to get that value later. The api docs don’t mention anything on how to do this. Also, I can’t derive the table name off the ActiveRecord class name because they are keyed differently than the table name.

Here is a better example of what I am doing

table_klass = Class.new(ActiveRecord::Base)
    ActiveRecord::Base.const_set(const_name,table_klass)
    app = @app
    table_klass.class_eval do
      after_save do
        @@channel.push self
      end
      set_table_name t.server_table
      establish_connection(
        :adapter  => "mysql2",
        :host     => app.db_host,
        :username => app.db_user,
        :password => app.db_pass,
        :database => app.db_name
      )
    end

In this case, if the const_name = Test and the database name is Database it should create a class of ActiveRecord::Base::DatabaseTest, which it does. But when I call table_name on it I get undefined local variable or method. Do I need to call table_name on the class?

Update:
I got it working by calling instance.class.table_name

  • 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-06-02T16:43:18+00:00Added an answer on June 2, 2026 at 4:43 pm

    Have you tried table_name? Docs.

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

Sidebar

Related Questions

In Rails, blocks can be used as callbacks , e.g.: class User < ActiveRecord::Base
I have a parent class in rails that inherits from ActiveRecord::Base. I'm trying to
I want to change the table name of the ActiveRecord model class during request
I have a user model: class User < ActiveRecord::Base end This model is used
I'm trying to create Class Table Inheritance as in ( http://www.castleproject.org/activerecord/documentation/trunk/usersguide/typehierarchy.html ) So let's
I have two dependent models with one to many association class Post < ActiveRecord::Base
I have the following code in an Active Record file. class Profile < ActiveRecord::Base
got 2 models: class User < ActiveRecord::Base has_many :posts end and class Post <
Hi (huge Rails newbie here), I have the following models: class Shop < ActiveRecord::Base
I have this Rails model: (Parameters removed for clarity) class Folder < ActiveRecord::Base belongs_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.