Unable to load model with names when calling rake task

I have a rake task that calls other rake tasks, so my development data can easily be reset.

first rake task (lib / tasks / populate.rake)

# Rake task to populate development database with test data
# Run it with "rake db:populate"
namespace :db do
  desc 'Erase and fill database'
  task populate: :environment do
    ...
    Rake::Task['test_data:create_company_plans'].invoke
    Rake::Task['test_data:create_companies'].invoke
    Rake::Task['test_data:create_users'].invoke
   ...
  end
end

second rake task (lib / tasks / populate_sub_scripts / create_company_plans.rake)

namespace :test_data do
  desc 'Create Company Plans'
  task create_company_plans: :environment do
    Company::ProfilePlan.create!(name: 'Basic', trial_period_days: 30, price_monthly_cents: 4000)
    Company::ProfilePlan.create!(name: 'Professional', trial_period_days: 30, price_monthly_cents: 27_500)
    Company::ProfilePlan.create!(name: 'Enterprise', trial_period_days: 30, price_monthly_cents: 78_500)
  end
end

when i run bin / rake db: populate then i get this error

rake is interrupted! LoadError: unable to auto-adjust constant Company :: ProfilePlan, expected /home/.../app/models/company/profile_plan.rb to determine it

but when I run the second rake task independently, it works well.

Model (path: / home /.../ app / models / company / profile_plan.rb)

class Company::ProfilePlan < ActiveRecord::Base
  # == Constants ============================================================

  # == Attributes ===========================================================

  # == Extensions ===========================================================
  monetize :price_monthly_cents

  # == Relationships ========================================================
  has_many :profile_subscriptions

  # == Validations ==========================================================

  # == Scopes ===============================================================

  # == Callbacks ============================================================

  # == Class Methods ========================================================

  # == Instance Methods =====================================================
end

Rails 5.0.1 Ruby 2.4.0

The app was just updated from 4.2 to 5

It works when I need it all the way:

require "#{Rails.root}/app/models/company/profile_plan.rb"

, . - , ?

+4
2

, , , , create_company_plans.rake, , , , , , .

QA, .

, , , :

require 'models/company/profile_plan'
+1

, , , , reenable ing, revoke , . , .

['test_data:create_company_plans', 'test_data:create_companies'].each do |task|
  Rake::Task[task].reenable
  Rake::Task[task].invoke
end

stackoverflow how-to-run-rake-tasks-from-within-rake-tasks.

+1

Source: https://habr.com/ru/post/1666853/


All Articles