I am trying to create a very basic Sinatra app which uses only Active Record and Sqlite3.
To be as complete as possible I am following instructions from a tutorial which states the following steps in order:
ActiveRecord::Base.establish_connection(
:adapter =>'sqlite3',
:database=>'wiki.db'
)
class User < ActiveRecord::Base
validates :username, presence: true, uniqueness: true
validates :password, presence: true
end
require "./wadapp.rb"
require "sinatra/activerecord/rake"
rake db:create_migration NAME=create_users
db/migrate/
called timestamp_create_users.rb
Navigate to that file and edit with the following contents:class CreateUsers < ActiveRecord::Migration[6.0]
def change
create_table :users do |t|
t.string :username
t.string :password
t.boolean :edit
t.timestamps null: false
end
User.create(username: "Admin", password: "admin", edit: true)
end
end
rake db:migrate
.It is this final point where the code stops working. I get no output at all to indicate a :users
has been created and no table is accessible within the app.
I have tried rolling back etc but there is no indication a table was even created so there is nothing roll back or alter? I also tried running rake db:create
first as some posts suggested but got the following error:
(in /Users/jonathonday/ruby/wad/wiki)
rake aborted!
ActiveRecord::AdapterNotSpecified: The `development` database is not configured for the `default_env` environment.
Available databases configurations are:
/Users/jonathonday/.rvm/gems/ruby-2.6.3/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/connection_specification.rb:251:in `resolve_symbol_connection'
/Users/jonathonday/.rvm/gems/ruby-2.6.3/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/connection_specification.rb:219:in `resolve_connection'
/Users/jonathonday/.rvm/gems/ruby-2.6.3/gems/activerecord-6.0.2.1/lib/active_record/connection_adapters/connection_specification.rb:140:in `resolve'
/Users/jonathonday/.rvm/gems/ruby-2.6.3/gems/activerecord-6.0.2.1/lib/active_record/connection_handling.rb:187:in `resolve_config_for_connection'
/Users/jonathonday/.rvm/gems/ruby-2.6.3/gems/activerecord-6.0.2.1/lib/active_record/connection_handling.rb:50:in `establish_connection'
/Users/jonathonday/.rvm/gems/ruby-2.6.3/gems/activerecord-6.0.2.1/lib/active_record/tasks/database_tasks.rb:187:in `create_current'
/Users/jonathonday/.rvm/gems/ruby-2.6.3/gems/activerecord-6.0.2.1/lib/active_record/railties/databases.rake:39:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:create
(See full trace by running task with --trace)
I have also run the command rake db:migrate as a root user (sudo su) and did get a different error:
(in /Users/jonathonday/ruby/wad/wiki)
rake aborted!
LoadError: cannot load such file -- sinatra
/Users/jonathonday/.rvm/rubies/ruby-2.6.3/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/Users/jonathonday/.rvm/rubies/ruby-2.6.3/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
This is the order I was given the instructions in to create and build the basic database with ActiveRecord so if anything is missing or there is a better way I will be very happy to listen.
I was having the same problem, and I believe I found the answer to your second question, why you had to revert to an older version of Active Record (5.2 instead of 6.0).
If you look at the sinatra-activerecord repo, it looks like they've deprecated ActiveRecord::Base.establish_connection() and replaced it with set :database{}.
So, if you change your code:
ActiveRecord::Base.establish_connection(:adapter =>'sqlite3', :database=>'wiki.db')
to
set :database, {:adapter =>'sqlite3', :database=>'wiki.db'}
Your code should work without having to use an older version of Active Record. At least that worked for me.