【问题标题】:ActiveRecord's configuration error - ActiveRecord::AdapterNotSpecifiedActiveRecord 的配置错误 - ActiveRecord::AdapterNotSpecified
【发布时间】:2017-03-27 23:11:37
【问题描述】:

程序为什么不运行?

list.rb

require 'active_record'
require 'yaml'

ActiveRecord::Base.configurations = YAML.load_file('./database.yml')
ActiveRecord::Base.establish_connection('development')

class Student < ActiveRecord::Base
end

student = Student.find('123')
puts student.id
puts student.name

数据库.yml

default: &default
  adapter: sqlite3
  encoding: unicode
  pool: 5

development:
  <<: *default
  database: my_database_name

list.db

sqlite> 从学生中选择 *;

123|foo|foo@email.com

错误

../activerecord-5.0.0.1/lib/active_record/connection_adapters/connection_specification.rb:170:in `spec': database configuration does not specify adapter (ActiveRecord::AdapterNotSpecified)

【问题讨论】:

标签: ruby activerecord


【解决方案1】:

在我的例子中,我使用字符串来标识我想要使用的数据库配置,而不是符号(Rails 5 似乎需要)

试试这个:

ActiveRecord::Base.establish_connection(:development)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 2014-06-13
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 2015-07-01
    • 1970-01-01
    相关资源
    最近更新 更多