循环依赖与自动加载

时间:2013-10-19 17:55:31

标签: ruby-on-rails ruby syntax dependencies classloader

我有一个超类锻炼:

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

我需要子类列表。每个子类都是这样的:

class WeeklyWorkout < Workout
   (...)
end

所以,当我加载Workout课程时,一切都很顺利。但是当我尝试加载任何子类(不首先加载Workout类)时,存在循环依赖的问题。例如:

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 `'

我知道问题发生的原因。但我无法弄清楚如何解决它。有谁知道如何设计这个来解决这个问题?

1 个答案:

答案 0 :(得分:2)

当你写:

SUBCLASSES = [WeeklyWorkout, DailyWorkout, OneTimeWorkout]

它自动加载各种文件。 (我猜你知道这一点。)

避免此问题的一种方法是使用符号:

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

然后,当您访问SUBCLASSES的元素时,请使用const_get。这将阻止子类文件与父类的文件一起自动加载。