Heroku Rake Does Not See Namedspaced Model In Rake Task

30 views Asked by At

I'm using rails 6.1.4 and ruby 3.1.1

In my app I have a section of it in a sub-directory: living_muay_thai/. I've created a rails task to populate a table with specific data. It works perfectly fine on my local machine, but when I push it up to Heroku, it bombs on a model name.

$ heroku run rails level_requirements_upload
...
DEBUG -- :   LivingMuayThai::LevelRequirement Load (0.9ms)  SELECT "living_muay_thai_level_requirements".* FROM "living_muay_thai_level_requirements"
rails aborted!
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "living_muay_thai_level_requirements" does not exist
LINE 1: ...LECT "living_muay_thai_level_requirements".* FROM "living_mu...

Models:

# Table name: living_muay_thai_levels
#
#  id         :bigint           not null, primary key
#  color      :string
#  level_name :string
#  sort_order :integer
#  created_at :datetime         not null
#  updated_at :datetime         not null
#
class LivingMuayThai::Level < ApplicationRecord

    has_many :living_muay_thai_level_requirements
    has_many :living_muay_thai_requirements, through: :living_muay_thai_level_requirements
    
end

--------------------------------------

# Table name: living_muay_thai_requirements
#
#  id               :bigint           not null, primary key
#  requirement_name :string
#  created_at       :datetime         not null
#  updated_at       :datetime         not null
#
class LivingMuayThai::Requirement < ApplicationRecord
    has_many :living_muay_thai_level_requirements
    has_many :living_muay_thai_levels, through: :living_muay_thai_level_requirements
end

--------------------------------------

# Table name: living_muay_thai_level_requirements
#
#  id             :bigint           not null, primary key
#  created_at     :datetime         not null
#  updated_at     :datetime         not null
#  level_id       :integer          not null
#  requirement_id :integer          not null
#
# Indexes
#
#  level_req_index  (level_id)
#  req_level_index  (requirement_id)
#
#
class LivingMuayThai::LevelRequirement < ApplicationRecord
  belongs_to :living_muay_thai_level, class_name: 'LivingMuayThai::Level', foreign_key: :level_id
  belongs_to :living_mauy_thai_requirement, class_name: 'LivingMuayThai::Requirement', foreign_key: :requirement_id

  validates_uniqueness_of :level_id,  scope: :requirement_id 
end

My rake task:

desc "create level requirements records"
task :level_requirements_upload => :environment do 

  LivingMuayThai::LevelRequirement.destroy_all
  LivingMuayThai::Requirement.destroy_all


    @reqs.each_line do |line|
      parts = line.split(",")
        parts[0].strip!
        parts[1].strip!

        puts "-------------------------"
        puts "part[0] => #{parts[0]} and parts[1] is #{parts[1]}"


        req=LivingMuayThai::Requirement.new(:requirement_name => parts[1])
        if req.save
        puts "Requirement #{parts[1]} entered"

        # we have levels in the db, so add the level requirement here
        level=LivingMuayThai::Level.where(:level_name => parts[0]).first
        if level.present?
            level_id = level.id
            lr=LivingMuayThai::LevelRequirement.new(:level_id => level_id, :requirement_id => req.id)
              if lr.save
                puts "Insert into level_requirements #{lr.level_id}, #{lr.requirement_id}"
              else
                puts "Error: #{lr.errors.full_messages}"
              end
        end 

        else
            puts "Error Requirement: #{req.errors.full_messages}"
        end
    end
end

@reqs = "Level 1, Class 1
...more lines..."

A line in the error output points me to line 4 in the rake task:

/app/lib/tasks/level_requirement_upload.rake:4:in `block in <top (required)>'

Line 4 is:

LivingMuayThai::LevelRequirement.destroy_all

That would coinside with the sql shown in the error. It shows the living_muay_thai_level_requirements table.

I've repeatedly checked the spelling on the table names in the models and rake task. The models(tables) on Heroku are empty.

I've tried to single quote the class names in the rake task and that bombs differently, as expected.

Any ideas what is causing this? Does Rails not know how to interpret living_muay_thai_level_requirements to the namespaced directory and models?

I've Googled for hours and cannot make heads or tails of this. Very little threads out there on namespacing issues.

Thanks for looking.

0

There are 0 answers