2013-02-13 186 views
1

我使用:我的班級正在調用一個不存在的班級?

  • 的Ruby 1.9.2
  • 的Rails 3.1.10

這是我的代碼:

class Report::ExpectedHour 

    def initialize(user, options = {}) 
    @user  = user 
    @date_start = options[:start] 
    @date_end = options[:end] 
    end 

    def expected_hours_range 
    previous = ExpectedHour.previous_dates(@user, @date_start).first 
    hours_range = ExpectedHour.between_dates(@user, @date_start, @date_end) 

    unless hours_range.include?(previous) 
     hours_range << previous 
    end 

    hours_range 
    end 

end 

每次我打電話expected_hours_range從我的實例我得到這個錯誤:

NameError: uninitialized constant Report::ExpectedHour::ExpectedHour 
from /home/edelpero/.rvm/gems/[email protected]/gems/aws-s3-0.6.2/lib/aws/s3/extensions.rb:206:in `const_missing_from_s3_library' 
from /opt/lampp/htdocs/titi/app/models/report/expected_hour.rb:10:in `expected_hours_range' 

我不確定爲什麼Report::ExpectedHour::ExpectedHour被調用,因爲我打電話ExpectedHour這是一個實際存在的ActiveRecord類。另外Report::ExpectedHour::ExpectedHour不存在。

+0

前綴: – 2013-02-13 14:13:10

回答

2

在你的類方法內部調用類時,ruby希望它是嵌套在你自己類中的類或者是一個常量。試試這個:

class MyClass 
    def some_method 
    use_external_class = ::ExternalClass::CONSTANTB.bla 
    # Use the '::' 
    end 
end 
+0

謝謝戴夫牛頓和nicooga爲你解釋,它的工作原理非常完美。我有另一個問題。那麼爲什麼在我的ActiveRecord類中,當調用另一個模型時,我不需要使用::?再次感謝。 – edelpero 2013-02-13 14:20:46

+0

不要在您的評論中提出另一個問題。搜索堆棧溢出的答案,並且,如果它沒有出現,爲這個特定的事情創建一個新的問題。 – 2013-02-13 14:26:53

相關問題