我正在按照子域進行數據庫更改的教程,該子域必須是系統將連接到的現有數據庫的名稱,如在012:在15:30分鐘中所示並工作相當好,問題來了,當系統訪問一個子域的數據庫不存在,給我看到以下錯誤:Cannot open database "another_database" requested by the login. The login failed
並顯示錯誤不允許訪問系統,無論是否訪問的名稱爲子域中的現有數據庫,除非導軌服務器重新啓動。嘗試更改數據庫時出錯
這是我在我的ApplicationController代碼:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :connect_to_database
def connect_to_user_database(name)
config = ActiveRecord::Base.configurations["development"].merge("database" => "#{name}")
ActiveRecord::Base.establish_connection(config)
#auth = name
#raise auth.to_yaml #podemos ver los datos que nos ofrece el parametro
end
private
def connect_to_database
connect_to_user_database(request.subdomains(0).first)
end
end
爲了安全起見並避免錯誤,您應該在'connect_to_database'之前檢查子域是否有效。 – wesley6j
@ wesley6j謝謝你的回答,但是如何檢查數據庫是否存在? –
如果您有要使用的子域列表,請將該數組保存在代碼庫或配置文件中的某處。 – wesley6j