Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

ActiveRecord.establish_connection uses class variable instead of accessor method

#1373

Is there a reason ActiveRecord.establish_connection uses @@connection_handler.establish_connection instead of connection_handler.establish_connection? I'm trying to make a subclass of ActiveRecord::Base that has it's own ConnectionHandler instance and I would want to leave ActiveRecord::Base's connection handler exactly the same. Here is what I'm trying to do:


class AnotherActiveRecord < ActiveRecord::Base
  @@another_connection_handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new
  def connection_handler
    @@another_connection_handler
  end
end

Would it be possible for establish_connection to use connection_handler accessor method so that this overloading can be done easier? Or is there a better way to do this?

Thanks!

Reported by ronin-37489 (at lighthouseapp) · November 14th, 2008 @ 06:30 AM

State: invalid
Milestone: 2.x
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Pratik
    Pratik
    • Assigned user set to Pratik
    • State changed from new to invalid

    You should just call establish_connection() from the model instead of overriding connection_handler.

    
    class Model < ActiveRecord::Base
      establish_connection ...
    end
    

    March 10th, 2009 @ 12:29 PM

  2. ronin-37489 (at lighthouseapp)
    ronin-37489 (at lighthouseapp)

    I would normally do just that. However, for this case, I need to connect to many databases with the same schema. Therefore, if the connection handler has a cache of connections based on the model name, then this would work. Ideally, the connection handler should cache on the hostname + database name + table. Or, in my case, overriding the connection handler with it's own cache.

    March 10th, 2009 @ 05:05 PM

  3. ronin-37489 (at lighthouseapp)
    ronin-37489 (at lighthouseapp)

    correction: ...then this would not work..

    March 10th, 2009 @ 05:06 PM