Rails Rake Task Not Seeing Model When Using NameSpacing for model relationships

39 Views Asked by At

rails 6.1.4 ruby 3.1.1

I have a sub-directory in my app: living_muay_thai I had a couple problems with my app because I was using the namespacing like so: LivingMuayThai::Student in the models. With some help here on StackOverflow I have those issues resolve. But I have another one with a similar issue that I need help on. I can't find any help via tutorials and Google, so I'm asking here again.

I have a rake task to pre-fill a table with data. It runs fine on my local machine, but not when I push it up to Herokku.

I tried using the shorter model names (Level, Requirement, LevelRequirement, but they did not work. Then I thought it might be that the rake task is not in the app/ directory. I tried to require the models, but that did not work. I'm stuck.

My rake file:

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

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

            req=LivingMuayThai::Requirement.create(:requirement_name => parts[1])

        # we have levels in the db, so add the level requirement here
        level=LivingMuayThai::Level.where(:level_name => parts[0]).first

        if level.present?
            lr=LivingMuayThai::LevelRequirement.create(:level_id => level.id, :requirement_id => req.id)
        end 

        end # reqs.each
    end # task
end

PG::UndefinedTable: ERROR: relation "living_muay_thai_level_requirements" does not exist

My models for this are in the living_muay_thai/ directory. I've changed them to use modules as instructed in another thread. That cleared up other problems, but then this one popped up.

Models:


# level

module LivingMuayThai
    class Level < ApplicationRecord

        has_many :student_levels
        has_many :students, through: :student_levels

        validates :color, presence: true
        validates :level_name, presence: true, uniqueness: true

        has_many :level_requirements
        has_many :requirements, through: :level_requirements
    end 
end



# requirement

module LivingMuayThai
    class Requirement < ApplicationRecord
        has_many :level_requirements
        has_many :levels, through: :level_requirements
        
        has_many :requirement_techniques
        has_many :techniques, through: :requirement_techniques
    end
end



#level_requirement

module LivingMuayThai
  class LevelRequirement < ApplicationRecord
    belongs_to :level, class_name: 'Level', foreign_key: :level_id
    belongs_to :requirement, class_name: 'Requirement', foreign_key: :requirement_id

    validates_uniqueness_of :level_id,  scope: :requirement_id 
  end
end

Thanks for any help and insights.

1

There are 1 best solutions below

0
John Cowan On

I have this working on Heroku now. @max replied to me in a similar thread here on Heroku: Heroku Rake Does Not See Namedspaced Model In Rake Task

He noticed the foreign keys on my joins tables were integer and not bigint, and he mentioned that Posgres might have a hissy-fit over these. I went through and created update migrations on all my joins tables to change these keys to bigints.

I ran the migrations on Heroku and ran the rake task. It worked great. My guess is Posgres is happy now.

# Example of change migration:

class ChangeIdsInLevelRequirements < ActiveRecord::Migration[6.1]
  def change 
    change_column :living_muay_thai_level_requirements, :level_id, :bigint
    change_column :living_muay_thai_level_requirements, :requirement_id, :bigint    
  end
end