Rails:在模型中指定不同的数据库

时间:2012-05-18 01:13:40

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

我正在尝试设置Rails(v3.2.2)以使用多个数据库。我是基于这个Connecting Rails 3.1 with Multiple Databases做的。

我的模特:

class Category < ActiveRecord::Base                                                                                                                                                                                                                                                                                                
  establish_connection :category_database                                                                                                                                                                                                                                                                                                                       
  self.primary_key = "cat_id"                                                                                                                                                   

  validates_presence_of :name, :display_name, :description, :icon, :image, :parent_category_id, :create_time                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
end

database.yml中:

category_database:                                                                                                                                                              
  adapter: mysql2                                                                                                                                                               
  encoding: utf8                                                                                                                                                                
  reconnect: false                                                                                                                                                              
  database: main_cat                                                                                                                                                         
  pool: 5                                                                                                                                                                       
  username: root                                                                                                                                                                
  password: blah                                                                                                                                                                    
  socket: /var/run/mysqld/mysqld.sock

当我运行此spec文件时:

require 'spec_helper'                                                                                                                                                           

describe Category do                                                                                                                                                            
  puts "ENV: #{Rails.env}"                                                                                                                                                      
  it { should validate_presence_of :name }                                                                                                                                      
  it { should validate_presence_of :display_name }                                                                                                                              
  it { should validate_presence_of :description }                                                                                                                               
  it { should validate_presence_of :icon }                                                                                                                                      
  it { should validate_presence_of :image }                                                                                                                                     
  it { should validate_presence_of :parent_category_id }                                                                                                                        
  it { should validate_presence_of :create_time }                                                                                                                               

end            
像这样:

>rspec /path/to/category_spec.rb  

我明白了:

/home/user/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:45:in `resolve_hash_connection': database configuration does not specify adapter (ActiveRecord::AdapterNotSpecified)

我也尝试过像这样设置establish_connection:

  establish_connection(                                                                                                                                                         
    :adapter => "mysql2",                                                                                                                                                       
    :encoding => "utf8",                                                                                                                                                        
    :reconnect => false,                                                                                                                                                      
    :database => "main_cat",                                                                                                                                                 
    :pool => 5,                                                                                                                                                                 
    :username => "root",                                                                                                                                                        
    :password => "blah",                                                                                                                                                            
    :socket => "/var/run/mysqld/mysqld.sock")

导致相同的异常。 (AdapterNotSpecified)

奇怪的是,如果我完全放弃了establish_connection并通过database.yml文件应用完全相同的连接配置,如下所示:

test:                                                                                                                                                                           
  adapter: mysql2                                                                                                                                                               
  encoding: utf8                                                                                                                                                                
  reconnect: false                                                                                                                                                              
  database: main_cat                                                                                                                                                         
  pool: 5                                                                                                                                                                       
  username: root                                                                                                                                                                
  password: blah                                                                                                                                                                    
  socket: /var/run/mysqld/mysqld.sock   

它有效。

似乎Rails完全忽略了establish_connection ......我是否缺少某些应用程序级配置设置?如何让Rails识别establish_connection,以便我可以将不同的模型放在不同的数据库中?

非常感谢!

2 个答案:

答案 0 :(得分:6)

事实证明,Rails需要存在默认配置。在这种情况下,database.yml文件仍然需要测试数据库。否则,Rails将抛出异常,因为它希望在模型初始化之前建立一些连接。加载默认配置后,Rails会继续初始化模型,然后通过establish_connection进行模型级数据库配置。

我希望这有助于其他人。我花了相当多的时间通过

运行调试器

activerecord-3.2.2 / lib / active_record / connection_adapters / abstract / connection_specification.rb

想出这个。

答案 1 :(得分:0)

出于同样的原因,我遇到了同样的错误,但是我确实有一个测试:我的database.yml中的配置;但问题出在我提供&#34;建立连接&#34;模型定义中的方法:

  establish_connection 'cm_inv_mgmt_' + Rails.env.downcase

虽然我为生产/登台和开发环境提供了database.yml的部分,但我错过了cm_inv_mgmt_test!