【问题标题】:Rails: specifying a different database in the modelRails:在模型中指定不同的数据库
【发布时间】:2013-07-11 12:49:41
【问题描述】:

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

数据库.yml:

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

当我运行这个规范文件时:

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(                                                                                                                                                         
    :adapter => "mysql2",                                                                                                                                                       
    :encoding => "utf8",                                                                                                                                                        
    :reconnect => false,                                                                                                                                                      
    :database => "main_cat",                                                                                                                                                 
    :pool => 5,                                                                                                                                                                 
    :username => "root",                                                                                                                                                        
    :password => "blah",                                                                                                                                                            
    :socket => "/var/run/mysqld/mysqld.sock")

这会导致相同的异常。 (AdapterNotSpecified)

奇怪的是,如果我完全放弃建立连接并通过 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 似乎完全忽略了建立连接...我是否缺少一些应用程序级别的配置设置或其他什么?如何让 Rails 识别建立连接,以便我可以将不同的模型放在不同的数据库中?

非常感谢!

【问题讨论】:

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


    【解决方案1】:

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

    我希望这对其他人有所帮助。我花了相当多的时间来运行调试器

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

    解决这个问题。

    【讨论】:

      【解决方案2】:

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

        establish_connection 'cm_inv_mgmt_' + Rails.env.downcase
      

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

      【讨论】:

        猜你喜欢
        • 2012-05-15
        • 1970-01-01
        • 2020-01-22
        • 2022-01-21
        • 1970-01-01
        • 2015-03-08
        • 2019-08-07
        • 1970-01-01
        • 2018-09-15
        相关资源
        最近更新 更多