activerecord 3.2.2多个连接的关联问题

时间:2012-03-22 05:31:24

标签: mysql ruby activerecord ruby-on-rails-3.1

有人可以指出当我遇到像这样的关联问题时可能会出现的一些问题吗?

我的课程是:

class BaseDB < ActiveRecord::Base
  self.abstract_class = true
  def self.load_configs
    self.configurations = YAML::load(load_file)
  end
  def self.load_file
    File.open(file_path)
  end
  def self.file_path
    File.join(File.expand_path('../../../',__FILE__),"config/database.yml")
  end
end

class User < BaseDB
has_many :things
end

class Thing < BaseDB
belongs_to :user
end

当我尝试在控制台中运行时

       >BaseDB.load_configs
        => {"my_connection"=>{"database"=>"db2", "adapter"=>"mysql2", "password"=>secret, "host"=>"localhost", "user"=>"root"}, "my_other_connection_test"=>{"database"=>"db1", "adapter"=>"mysql2", "password"=>secret, "host"=>"localhost", "user"=>"root"}}
       > BaseDB.establish_connection :my_connection
        => #<ActiveRecord::ConnectionAdapters::ConnectionPool:0x7f69c7a38b98 @mon_entering_queue=[], @automatic_reconnect=true, @mon_count=0, @queue=#<MonitorMixin::ConditionVariable:0x7f69c7a38a58 @waiters=[], @monitor=#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x7f69c7a38b98 ...>>, @connections=[], @mon_owner=nil, @reserved_connections={}, @spec=#<ActiveRecord::Base::ConnectionSpecification:0x7f69c7a49718 @adapter_method="mysql2_connection", @config={:password=>"####", :adapter=>"mysql2", :user=>"root", :host=>"localhost", :database=>"db2"}>, @timeout=5, @size=5, @mon_waiting_queue=[]>

       > user = User.first
        =>  #<User id: 8325205, firstname: "goryo", middlename: nil, creeated_at: "2012-03-20 18:02:58", updated_at: "2012-03-20 18:02:58">

       > user.things

然后我得到这些错误:

ActiveRecord::ConnectionNotEstablished: ActiveRecord::ConnectionNotEstablished
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:374:in `retrieve_connection'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:168:in `retrieve_connection'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:142:in `connection'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/alias_tracker.rb:75:in `connection'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/alias_tracker.rb:54:in `initial_count_for'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/alias_tracker.rb:12:in `initialize'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/alias_tracker.rb:29:in `call'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/alias_tracker.rb:29:in `default'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/alias_tracker.rb:29:in `[]'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/alias_tracker.rb:29:in `aliased_name_for'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/alias_tracker.rb:17:in `aliased_table_for'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/join_helper.rb:15:in `construct_tables'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/join_helper.rb:14:in `each'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/join_helper.rb:14:in `construct_tables'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/association_scope.rb:37:in `add_constraints'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/association_scope.rb:31:in `scope'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/association.rb:98:in `association_scope'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/association.rb:87:in `scoped'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/collection_association.rb:380:in `find_target'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/collection_association.rb:333:in `load_target'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/collection_proxy.rb:44:in `__send__'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/collection_proxy.rb:44:in `load_target'
        from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/collection_proxy.rb:87:in `method_missing'
        from /usr/lib/ruby/1.8/irb.rb:310:in `output_value'
        from /usr/lib/ruby/1.8/irb.rb:159:in `eval_input'
        from /usr/lib/ruby/1.8/irb.rb:271:in `signal_status'
        from /usr/lib/ruby/1.8/irb.rb:155:in `eval_input'
        from /usr/lib/ruby/1.8/irb.rb:154:in `eval_input'
        from /usr/lib/ruby/1.8/irb.rb:71:in `start'
        from /usr/lib/ruby/1.8/irb.rb:70:in `catch'
        from /usr/lib/ruby/1.8/irb.rb:70:in `start'

1 个答案:

答案 0 :(得分:1)

哦,我需要的是将ActiveRecord :: Base连接建立为初始以便连接到其他表,它可以工作!

ActiveRecord::Base.establish_connection :my_connection

因为我的basedb是一个抽象类,当你建立一个连接ActiveRecord :: Base w / c是超类没有连接但你可以检查出来

>BaseDB.load_configs
>BaseDB.establish_connection :my_connection

尝试查询

>User.all
>BaseDB.connected?
=>returns true
>ActiveRecord::Base.connected?
=>nil

有关实施的更多信息,请查看http://api.rubyonrails.org/classes/ActiveRecord/Base.html