在ActiveRecord模型上使用establish_connection时指定AdapterNot

时间:2016-07-18 11:25:40

标签: ruby-on-rails ruby database activerecord

这是一个示例模型。

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:数据库配置未指定适配器

1 个答案:

答案 0 :(得分:4)

要使其发挥作用,您需要更改:

class MyModel < ApplicationRecord
    establish_connection "other_db_#{Rails.env}"
end

class MyModel < ApplicationRecord
    establish_connection "other_db_#{Rails.env}".to_sym
end