尝试使用多个数据库 - 未建立ActiveRecord连接

时间:2011-10-21 04:57:05

标签: ruby activerecord

我正在尝试设置ActiveRecord,以便在我的应用程序中使用多个数据库连接。我使用Rails。

为此,我为每个数据库设置了一个抽象类:

  class BuoyDatabase < ActiveRecord::Base
    self.abstract_class = true
    establish_connection $database_config['buoy_database']
  end

然后我从OneDatabase类继承:

  class Buoy < BuoyDatabase
    has_many :buoyDatas, :foreign_key => 'buoy_id'
  end

  class BuoyData < BuoyDatabase
    belongs_to :buoy
  end

我可以成功创建和读取Buoy和BuoyData的实例,但我无法获得任何相关记录:

Buoy.find_by_id(...).buoyDatas  # gives ActiveRecord::ConnectionNotEstablished

错误详情:

ActiveRecord::ConnectionNotEstablished: ActiveRecord::ConnectionNotEstablished
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:409:in `retrieve_connection'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_specification.rb:107:in `retrieve_connection'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_specification.rb:89:in `connection'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/alias_tracker.rb:69:in `connection'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/alias_tracker.rb:54:in `initial_count_for'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/alias_tracker.rb:12:in `block in initialize'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/alias_tracker.rb:29:in `yield'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/alias_tracker.rb:29:in `default'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/alias_tracker.rb:29:in `aliased_name_for'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/alias_tracker.rb:17:in `aliased_table_for'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/join_helper.rb:15:in `block in construct_tables'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/join_helper.rb:14:in `each'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/join_helper.rb:14:in `construct_tables'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/association_scope.rb:53:in `add_constraints'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/association_scope.rb:33:in `scope'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/association.rb:99:in `association_scope'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/association.rb:88:in `scoped'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/collection_association.rb:367:in `find_target'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/collection_association.rb:324:in `load_target'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/collection_proxy.rb:51:in `load_target'
from /Users/admin/.rvm/rubies/ruby-1.9.3-head/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0/lib/active_record/associations/collection_proxy.rb:89:in `method_missing'

1 个答案:

答案 0 :(得分:2)

解决方法是在ActiveRecord :: Base上建立连接。连接到哪个数据库并不重要。仅需要此连接,以便ActiveRecord可以获取处理关联等所需的元信息。一旦建立,您根本不需要使用连接。

此问题与ActiveRecord 3.1.0 multiple databases重复,此处首先介绍了解决方法:https://stackoverflow.com/a/7406374/29729