这是一个示例模型。
class MyModel < ApplicationRecord
establish_connection "other_db_#{Rails.env}"
end
这是database.yml文件。
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
other_db_development:
<<: *default
database: db/my_other_database_development.sqlite
other_db_production:
<<: *default
database: db/my_other_database_production.sqlite
当我尝试访问模型时,我收到以下错误:
ActiveRecord :: AdapterNotSpecified:数据库配置未指定适配器
答案 0 :(得分:4)
要使其发挥作用,您需要更改:
class MyModel < ApplicationRecord
establish_connection "other_db_#{Rails.env}"
end
到
class MyModel < ApplicationRecord
establish_connection "other_db_#{Rails.env}".to_sym
end