Search code examples
ruby-on-railsrubyrakerake-task

Rails - can not access table from within rake task


I am trying to create a rake task which queries an API and adds records to a table. However, I can not access that table (I get the following error).

ActiveRecord::StatementInvalid: Could not find table 'exchange_rates'

My code is:

namespace :query_api do

  task get_currency_values: :environment do

    exchange_rate = ExchangeRate.new
    #some other code

  end

end

The code is located in

lib/tasks/query-api.rake

Solution

  • Agree with these other comments. This is a straightforward error. If you load up your rails console (rails c) in the environment in question (development or production or whatever) and type ExchangeRate.new then you should see the same error.

    From there investigate the connection to the DB and make sure it's actually working.

    Also, check which migrations have run in the environment where you're experiencing the error with rake db:migrate:status