2013-10-19 73 views
0

循環依賴我有一個超類鍛鍊: 與自動加載

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

而我需要的子類的列表。每個子類是這樣的:

class WeeklyWorkout < Workout 
    (...) 
end 

所以,當我加載的鍛鍊類一切順利。但是,當我嘗試加載任何子類(不首先加載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 `' 

我知道問題出現的原因。但我無法弄清楚如何解決它。有誰知道如何設計這個來解決這個問題?

回答

2

當你寫:

SUBCLASSES = [WeeklyWorkout, DailyWorkout, OneTimeWorkout] 

它自動加載的各種文件。 (我想你知道這一點),以避免問題

一種方法是用符號:

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

然後,當你訪問子類的元素,使用const_get。這將阻止子類文件自動加載父類的文件。

+0

這是一個有點解決方案。我知道這一點,但我想要更令人滿意的東西。在這裏,我必須在每次使用它時遍歷列表。 – pablo