1

我正在使用:

  • 红宝石 1.9.2
  • 导轨 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/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存在。

4

1 回答 1

2

在类方法中调用类时,ruby 期望它是嵌套在类本身中的类或常量。试试这个:

class MyClass
  def some_method
    use_external_class = ::ExternalClass::CONSTANTB.bla
    # Use the '::'
  end
end
于 2013-02-13T14:14:23.630 回答