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
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 what you need? Alain -- Posted via http://www.ruby-forum.com/.
Stephen Bannasch
2006-Apr-11 15:38 UTC
[Rails] ActiveRecord problem, 2 connections, only one w/table_name_prefix
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", :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. -- - Stephen Bannasch Concord Consortium, http://www.concord.org