1
votes

I have a superclass Workout:

class Workout < ActiveRecord::Base
  SUBCLASSES = [WeeklyWorkout, DailyWorkout, OneTimeWorkout]
  (...)
end

And I need the list of sub-classes. Each of sub-class is like this:

class WeeklyWorkout < Workout
   (...)
end

So, when I load Workout class everything goes fine. But when I try to load any of sub-classes (without loading Workout class first) there is a problem with circular dependencies. For example:

irb(main):062:0> WeeklyWorkout
RuntimeError: Circular dependency detected while autoloading constant WeeklyWorkout
    from /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:460:in `load_missing_constant'
    from /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:183:in `const_missing'
    from /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:494:in `load_missing_constant'
    from /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:183:in `const_missing'
    from /home/pablo/rails/tobefit/app/models/workout.rb:2:in `'
    from /home/pablo/rails/tobefit/app/models/workout.rb:1:in `'
    from /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:423:in `load'
    from /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:423:in `block in load_file'
    from /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:615:in `new_constants_in'
    from /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:422:in `load_file'
    from /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:323:in `require_or_load'
    from /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:462:in `load_missing_constant'
    from /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:183:in `const_missing'
    from /home/pablo/rails/tobefit/app/models/workouts/weekly_workout.rb:1:in `'
    from /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:423:in `load'
    from /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:423:in `block in load_file'
    from /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:615:in `new_constants_in'
    from /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:422:in `load_file'
    from /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:323:in `require_or_load'
    from /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:462:in `load_missing_constant'
    from /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:183:in `const_missing'
    from (irb):62
    from /var/lib/gems/1.9.1/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
    from /var/lib/gems/1.9.1/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
    from /var/lib/gems/1.9.1/gems/railties-4.0.0/lib/rails/commands.rb:64:in `'
    from bin/rails:4:in `require'
    from bin/rails:4:in `'

I know why the problem occurs. But I can't figure out how to fix it. Do anyone know how to design this to fix this problem?

1

1 Answers

3
votes

When you write:

SUBCLASSES = [WeeklyWorkout, DailyWorkout, OneTimeWorkout]

It autoloads the various files. (I presume you know this.)

One way to avoid the problem is to use symbols:

SUBCLASSES = [:WeeklyWorkout, :DailyWorkout, :OneTimeWorkout]

Then, when you access elements of SUBCLASSES, use const_get. This will prevent the subclass files from autoloading alongside the parent class's file.