1

我有一个超类锻炼:

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

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

class WeeklyWorkout < Workout
   (...)
end

所以,当我加载锻炼课程时,一切都很好。但是当我尝试加载任何子类(没有先加载 Workout 类)时,循环依赖存在问题。例如:

irb(main):062:0> 每周锻炼
RuntimeError:自动加载常量 WeeklyWorkout 时检测到循环依赖
    来自 /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:460:in `load_missing_constant'
    来自 /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:183:in `const_missing'
    来自 /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:494:in `load_missing_constant'
    来自 /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:183:in `const_missing'
    来自 /home/pablo/rails/tobefit/app/models/workout.rb:2:in `'
    来自 /home/pablo/rails/tobefit/app/models/workout.rb:1:in `'
    来自 /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:423:in `load'
    来自 /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:423:in `block in load_file'
    来自 /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:615:in `new_constants_in'
    来自 /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:422:in `load_file'
    来自 /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:323:in `require_or_load'
    来自 /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:462:in `load_missing_constant'
    来自 /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:183:in `const_missing'
    来自/home/pablo/rails/tobefit/app/models/workouts/weekly_workout.rb:1:in `'
    来自 /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:423:in `load'
    来自 /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:423:in `block in load_file'
    来自 /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:615:in `new_constants_in'
    来自 /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:422:in `load_file'
    来自 /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:323:in `require_or_load'
    来自 /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:462:in `load_missing_constant'
    来自 /var/lib/gems/1.9.1/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:183:in `const_missing'
    来自 (irb):62
    来自 /var/lib/gems/1.9.1/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
    来自 /var/lib/gems/1.9.1/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
    来自 /var/lib/gems/1.9.1/gems/railties-4.0.0/lib/rails/commands.rb:64:in `'
    从 bin/rails:4:in `require'
    从 bin/rails:4:in `'

我知道为什么会出现问题。但我不知道如何解决它。有谁知道如何设计这个来解决这个问题?

4

1 回答 1

3

当你写:

SUBCLASSES = [WeeklyWorkout, DailyWorkout, OneTimeWorkout]

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

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

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

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

于 2013-10-19T18:41:51.300 回答