I am building a Sinatra app and trying to use ActiveRecord. Rake is not recognizing my controller file and returning rake aborted! LoadError: cannot load such file whenever I run any rake task. Here are my gems:
source "https://rubygems.org"
ruby "2.7.2"
gem 'sinatra'
gem 'thin'
gem 'shotgun'
gem 'require_all'
gem 'activerecord'
gem 'sinatra-activerecord'
gem 'sqlite3'
gem 'rake', '~> 13.0.6'
And here's the Rakefile:
# Rakefile
require 'sinatra/activerecord/rake'
require_relative './app/test_controller.rb'
Any ideas? In the Rakefile, I've tried require_relative for ./app, /app, ../app, etc. Also, I'm on a bit of a time crunch, so if I can't sort this out quickly do I need Rake to use ActiveRecord?
TL/DR:
remove
require_relative './app/test_controller.rb'from your Rakefile.Details:
Rake is a tool that lets us define and run commands from the command line (e.g.
rake db:migrate). Activerecord comes bundled with a few rake tasks (like generating a migration file, running one, dumping/loading your database schema, etc) that make it much easier to work with.Rake tasks are just ruby, so to run a Rake task successfully it will need to load any classes/files it depends on (which is typically handled independently by each rake task). If you're going to write your own rake tasks you'll want to ensure that the tasks require the relevant files. Loading your app (or sinatra controllers) in a base Rakefile wouldn't generally be advised as it wouldn't always be needed.
The exception to this would be if you needed to load some kind of 'boot' or 'config' file in order to setup your database connections/configuration so that the activerecord rake tasks could successfully connect to your database.