gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 08:05:47 24 4
gpt4 key购买 nike

我正在尝试设置 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'

最佳答案

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

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

关于ruby - 尝试使用多个数据库 - 未建立 ActiveRecord 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7845126/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com