我的班级正在打电话给一个不存在的班级?

时间:2013-02-13 14:10:36

标签: ruby ruby-on-rails-3

我正在使用:

  • 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/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也不存在。

1 个答案:

答案 0 :(得分:2)

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

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