2

I have a project with a Vue frontend and a rails backend. It's just a very simple API on the backend, no database or anything. It's working fine locally but now I want to deploy it on Heroku.

However, when I run it I get the following error.

-----> Detecting rake tasks
 !
 !     Could not detect rake tasks
 !     ensure you can run `$ bundle exec rake -P` against your app
 !     and using the production group of your Gemfile.
 !     rake aborted!
 !     URI::InvalidURIError: bad URI(is not URI?): ://user:pass@127.0.0.1/dbname
  ...
  ...
/activerecord-6.0.2.1/lib/active_record/railties/databases.rake

Based on various SO posts/Heroku documentation, I have already tried:

  • bundle exec rake -P RAILS_ENV=production - everything looks ok
  • adding the rake dependency in Gemfile
  • removing the sqlite dependency in Gemfile
  • removing the BUNDLED WITH from Gemfile.lock

But still the same error.

I guess it's related to my database config, but I don't have any database on my project so this seems like an unnecessary task anyway. I tried commenting out railties from my Gemfile but it's still there as a dependency for other gems. When I deploy after making this change, it still hits the same task and fails.

Link to repo branch

Chris A
  • 863
  • 1
  • 10
  • 31

2 Answers2

1

Instead of require 'rails/all' which requires all the Railties including ActiveRecord you need to explicitily require the railties you want to use:

require File.expand_path('../boot', __FILE__)

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
# require "active_record/railtie"
# require "active_storage/engine"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_mailbox/engine"
# require "action_text/engine"
require "action_view/railtie"
require "action_cable/engine"
require "sprockets/railtie"

module Draw
  class Application < Rails::Application
    # You don't need this nonsense since you don't even have config/application.yml
    #  ENV.update YAML.load_file('config/application.yml')[Rails.env] rescue {}
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    # config.i18n.default_locale = :de

    # Do not swallow errors in after_commit/after_rollback callbacks.
    # config.active_record.raise_in_transactional_callbacks = true
  end
end

If you don't want to use a ActiveRecord you can just get rid of /db and /config/database.yml.

You also don't need to add have gem 'rake' in your Gemfile as rails depends on it anyways.

max
  • 96,212
  • 14
  • 104
  • 165
  • 1
    I'm not sure what railties I *do* want to use, if any... I'm not even sure what railties are. – Chris A Feb 02 '20 at 20:34
  • I also just noticed, as this is an implementation of Rails as an API, my application controller should extend `ActionController::API` instead of `::Base`. I don't know why it's Base as I specified --api when creating the app – Chris A Feb 02 '20 at 21:06
  • It still happens even after only requiring those specific railties – Chris A Feb 02 '20 at 21:59
  • Have you for some reason overridden `ENV["DATABASE_URL"]`? – max Feb 03 '20 at 05:56
  • There really is so little to this app though that I wonder if you shouldn't just start fresh and generate a new app with the options to skip ActiveRecord make it an API app. – max Feb 03 '20 at 06:22
  • I created a new API app and then brought in the stuff I created for the old app and the result was the same. Could you explain what you mean by "skip ActiveRecord"? – Chris A Feb 03 '20 at 10:10
  • 1
    `rails new app_name --skip-active-record --api` – max Feb 03 '20 at 10:27
  • If in the future I decide I want use active record, could this cause complications? – Chris A Feb 03 '20 at 10:29
  • 1
    You would have to re-add add the railtie, a database configuration and the options for active record in the config files. In that case you might just want to run `rails new app_name --api --database=postgresql` and just set it up from the get go. You might want to to try deploying before you migrate your code over so that you don't just reintroduce any potential bugs. – max Feb 03 '20 at 10:39
  • Thanks for the info, @max, I will have to think about whether I will need a database in future – Chris A Feb 03 '20 at 11:25
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/207105/discussion-between-chris-a-and-max). – Chris A Feb 03 '20 at 13:42
0

Needed to fully remove use of ActiveRecord from the project. As Max commented, in a new app this can be done by doing rails new app_name --skip-active-record --api, to do this for an existing project see this explanation

Chris A
  • 863
  • 1
  • 10
  • 31