"database configuration does not specify adapter" when using mysql in sinatra

Asked 2 years ago, Updated 2 years ago, 130 views

Let me ask you a question.
I am having trouble understanding the error below.

environmental framework:sinatra
working with:Activerecord
DB:mysql

[database.yml]

development:
    adapter —mysql2
    database —sinatra
    host —localhost
    username —user_1
    password —user_1
    encoding —utf8

】Contents of the file for bundle exec ruby <

require 'active_record'
require 'mysql2'
require'sinatra'

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

class Topic <ActiveRecord::Base
end

get'/topics.json'do
  content_type:json, :charset=>'utf-8'
  topics = Topic.order("pass").limit(1)

  status202
end

[Error Contents]

/Documents/sinatra/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.6/lib/active_record/connection_adaptors/abstract/connection_pool.rb: 880:in `establish_connection'

/Documents/sinatra/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.6/lib/active_record/connection_handling.rb:58:in `establish_connection' from a.rb:7:in`<main>'

/Documents/sinatra/vendor/bundle/ruby/2.3.0/gems/activerecord-5.1.6/lib/active_record/connection_adaptors/connection_specification.rb:182:in 
`spec': database configuration does not specify adapter (ActiveRecord:: AdapterNotSpecified)

[Database contents]

mysql>select * from user;
+--------+--------+
| id|pass|
+--------+--------+
| user_1 | user_1 |
+--------+--------+

What exactly is the problem with this error?
I looked it up, but I'm at a loss because I can only find the explanation of rails.
As for the item to be called, it says pass, but we aim to get the data first, so please don't worry about it.

I apologize for the careless explanation, but I appreciate your cooperation.

ruby sinatra

2022-09-30 14:42

1 Answers

ActiveRecord::Base.establish_connection('development')

in the section
ActiveRecord::Base.establish_connection(:development)

Change to or without ActiveRecord::Base.configuration

config=YAML.load_file('./database.yml')
ActiveRecord::Base.establish_connection(config ['development'])

How about


2022-09-30 14:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.