<Wayne_Hearn-DYMqY+WieiM@public.gmane.org>
2005-Apr-29 20:31 UTC
Multiple database connections
I''ve been trying to use the multiple database connections feature in ActiveRecord but I keep running into issues. Has anyone gotten this to work? I have a class A that I want to use as a base class for B and C. I have defined a method establish_connection in A: class A < ActiveRecord::Base CONFIG = ActiveRecord::Base::ConnectionSpecification.new( {:adapter => "oci", :host => "myHost", :username => "USERNAME", :password => "PASSWORD " }, "oci_connection") def A.establish_connection(spec=nil) self.connection = CONFIG end end Class B extends A. Class C extends A Class B has_one C Class C depends_on B In my controller for B I have defined a before_filter that calls A.establish_connection. So all is well when I hit the scaffolded views for B or C independently, but if I try to access B.C then ActiveRecord defaults to the default connection and doesn''t use the one I defined in the base class A. Does anyone know if I''m setting this up correctly? I tried calling super.establish_connection(CONFIG) from within Class A''s establish_connection method but it throws an exception: undefined method `establish_connection'' for #<ActiveRecord::Base::ConnectionSpecification:0x4049370> Any help would be greatly appreciated, I''ve spent a couple days on this already. Thanks, Wayne Hearn
Wayne_Hearn-DYMqY+WieiM@public.gmane.org wrote:> I''ve been trying to use the multiple database connections feature in > ActiveRecord but I keep running into issues. Has anyone gotten this to > work? > I have a class A that I want to use as a base class for B and C. I have > defined a method establish_connection in A: > > class A < ActiveRecord::Base > CONFIG = ActiveRecord::Base::ConnectionSpecification.new( > {:adapter => "oci", > :host => "myHost", > :username => "USERNAME", > :password => "PASSWORD " > }, > "oci_connection") > > def A.establish_connection(spec=nil) > self.connection = CONFIG > end > endYou don''t need any special code in your class. in config/database.yml: oci_connection: adapter: oci host: myHost username: USERNAME password: PASSWORD in config/environment.rb: A.establish_connection :oci_connection or in app/controllers/my_controller: before_filter { A.establish_connection(:oci_connection) unless A.connected? } This connection *applies to A only*. If you want B and C to use it, explicitly set them up: [B, C].each { |klass| klass.connection = A.connection } Best, jeremy