对象找不到后代?

时间:2014-02-24 17:00:10

标签: ruby-on-rails ruby inheritance descendant

我正在编写一个类来编写名为“BravoManagementXlsReport”的XLS文件。它目前位于以下目录中:

enter image description here

所有其他报告都继承自xls_report,所以我做了同样的事情。

class BravoManagementXlsReport < XlsReport
  attr_reader :file_name
  ...
end

当我跑完工人时,我得到了:

2014-02-24T16:22:58Z 18204 TID-ovbv17qdg WARN: uninitialized constant BravoManagementXlsReport

我认为我应该重新启动工作程序,但这不起作用,所以我重新启动了Rails服务器,但这也没有用。在控制台中我尝试了:

[2] toolkit »  ManagementXlsReport // this returns the object
=> ManagementXlsReport < XlsReport
[3] toolkit »  BravoManagementXlsReport // this is obviously not finding it
NameError: uninitialized constant BravoManagementXlsReport

[1] toolkit »  XlsReport.descendants // this returns everything but the new file
=> [
  [0] SocioeconomicDevelopmentXlsReport < XlsReport,
  [1] EnterpriseDevelopmentXlsReport < XlsReport,
  [2] PreferentialProcurementXlsReport < XlsReport,
  [3] IntermediaryBillXlsReport < XlsReport,
  [4] ScorecardXlsReport < XlsReport,
  [5] TrainingProgramXlsReport < XlsReport,
  [6] GeneralXlsReport < XlsReport,
  [7] EmploymentEquityXlsReport < XlsReport,
  [8] ManagementXlsReport < XlsReport,
  [9] SkillsDevelopmentXlsReport < XlsReport
]

我有什么遗失的吗?我试着看看我是否拼错了但是我看不到它。

1 个答案:

答案 0 :(得分:0)

我终于知道发生了什么。路径没有被rails加载。在应用程序配置文件中我找到了这个。我认为它只是糟糕的遗留代码让我感到困惑。

# These are imported so that the elements are downloadable to excel
require "#{Rails.root}/lib/reports/xls_report.rb"
[:employment_equity, :management, :skills_development, :enterprise_development, :preferential_procurement, :socioeconomic_development, :general, :training_program, :scorecard, :intermediary_bill].each do |file|
  require "#{Rails.root}/lib/reports/#{file}_xls_report.rb"
end

所以我只是用自动路径加载器替换它:

config.autoload_paths += %W( #{Rails.root}/lib/reports )

现在它工作得很好:)非常感谢您的帮助!希望这最终会帮助别人。