我正在使用:
这是我的代码:
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/ruby-1.9.2-p180@titi/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 :(得分:2)
在类方法中调用类时,ruby希望它是嵌套在类本身内的类或常量。试试这个:
class MyClass
def some_method
use_external_class = ::ExternalClass::CONSTANTB.bla
# Use the '::'
end
end