# AR class names

**URL:** <https://www.ruby-forum.com/t/ar-class-names/93825>\
**Category:** Rails\
**Created:** [April 11, 2006, 3:35pm UTC](https://www.ruby-forum.com/t/ar-class-names/93825 "2006-04-11T15:35:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![orotone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/90db22/32.png) [@orotone](https://www.ruby-forum.com/u/orotone)\
**Post date:** [April 11, 2006, 3:35pm UTC](https://www.ruby-forum.com/t/ar-class-names/93825/1 "2006-04-11T15:35:37Z")

</div>

hello all, quick question: how can I find out if a given string matches  
the  
name of an ActiveRecord derived class?

thanks in advance  
alan

---

<div class="post-metadata">

**Author:** ![orotone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/90db22/32.png) [@orotone](https://www.ruby-forum.com/u/orotone)\
**Post date:** [April 11, 2006, 4:18pm UTC](https://www.ruby-forum.com/t/ar-class-names/93825/2 "2006-04-11T16:18:51Z")

</div>

Alan

> hello all, quick question: how can I find out if a given string  
> matches  
> the name of an ActiveRecord derived class?

Is  
[http://nubyonrails.com/tools/pluralize](http://nubyonrails.com/tools/pluralize)  
what you need?

Alain

---

<div class="post-metadata">

**Author:** ![orotone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/90db22/32.png) [@orotone](https://www.ruby-forum.com/u/orotone)\
**Post date:** [April 11, 2006, 5:38pm UTC](https://www.ruby-forum.com/t/ar-class-names/93825/3 "2006-04-11T17:38:24Z")

</div>

I’d like to use ActiveRecord to access my regular tables in a shared  
database using table\_name\_prefix AND at the same time access a legacy  
database with table\_name\_prefix set to “”. This is not easy because  
class variables like table\_name\_prefix can’t be redefined in derived  
classes without changing the class variable in the parent class.

I can workaround this problem by using set\_table\_name in my models  
for tables in the shared database like this:

class User \< ActiveRecord::Base  
set\_table\_name “myapp\_users”

But now my existing migrations won’t work and I have to hack them now  
too.

Details are below.

--------details--------

I have two different ActiveRecord connections open and one of them is  
to a shared database so I set the following class variable:

ActiveRecord::Base.table\_name\_prefix = ‘myapp\_’

So while I refer to the table ‘users’ in my code in the database the  
table is actually named ‘myapp\_users’.

However the other connection is to a legacy database and I need to  
access the tables in this database without the table\_name\_prefix.  
Here’s my model for the legacy connection:

class LegacyUser \< ActiveRecord::Base  
set\_primary\_key “user\_id”  
cattr\_accessor :table\_name\_prefix  
@@table\_name\_prefix = “”  
unless connected?  
establish\_connection(  
:adapter =\> “mysql”,  
:host =\> “[database.host.com](http://database.host.com)”,  
:username =\> “rails”,  
:password =\> “\*\*\*\*\*\*\*”,  
:database =\> “legacydb”  
)  
end  
end

Unfortunately redefining table\_name\_prefix as a class variable in the  
derived class LegacyUser actually changes the class variable in the  
parent class also (ActiveRecord::Base).

Right now I can either connect to a shared database and access all my  
tables using the table\_name\_prefix set properly OR access the legacy  
database with table\_name\_prefix set to “” BUT I can’t do both.

If I don’t redefine table\_name\_prefix I get the following error when  
accessing LegacyUser:

Table ‘legacydb.myapp\_legacy\_users’ doesn’t exist:

If I do redefine table\_name\_prefix I get the following error when  
accessing User:

Table ‘regulardb.users’ doesn’t exist

Because of course the table name is actually: regulardb.myapp\_users.

Here the simple ruby test program I created to determine the  
redefinition scope of class variables:

class Klass  
def prefix  
@@prefix  
end  
def prefix=(value)  
@@prefix=value  
end  
def Klass.show(string)  
puts @@prefix + string  
end  
end

puts Klass.prefix.object\_id

class DerivedKlass \< Klass  
def prefix  
@@prefix  
end  
def prefix=(value)  
@@prefix=value  
end  
end

puts DerivedKlass.prefix.object\_id

Klass.prefix = “123”  
Klass.show(“abc”) # =\> “123abc”  
DerivedKlass.prefix = “789”  
DerivedKlass.show(“abc”)# =\> “789abc”  
Klass.show(“abc”) # =\> “789abc”

which produces:

2968188  
2968188  
123abc  
789abc  
789abc

I would like to find a way so that the second: Klass.show(“abc”)  
results in “123abc” instead of “789abc”.

I’d like to be able to create a class variable that will apply to all  
objects made from that class and ALSO create a derived class with a  
different class variable that is used in objects created from the  
derived class WITHOUT affecting the class variable defined in the  
original class.

It isn’t obvious to me how to do this using class variables so either  
I’m missing something or there is another way to solve this  
programming problem.

If this can’t be done perhaps I could do something in DerivedKlass so  
that any accesses to it’s actual or inherited object methods save  
Klass.prefix, replace it, and restore it? Of course the restoration  
would have to work for both normal program flow and for an error.  
This sounds uglier to me.
