Search code examples
ruby-on-railsrubyinheritancedescendant

Object can't find descendent?


I'm writing a class to write XLS files called "BravoManagementXlsReport". It's currently sitting in the following directory:

enter image description here

All the other reports all inherit from xls_report, so I did the same.

class BravoManagementXlsReport < XlsReport
  attr_reader :file_name
  ...
end

When I run the worker I get:

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

I thought I should restart the workers, but that didn't work, so I restarted the Rails server but that didn't work either. In the console I tried:

[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
]

Is there something I am missing? I tried seeing if I had misspelled something but I can't see it.


Solution

  • I finally found out what was going on. The path was not being loaded by rails. In the application config file i found this. i think its just bad legacy code that got me confused.

    # 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
    

    So i just replaced it with a auto path loader:

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

    now it works just fine :) thanks so much for all your help this! hope this ends up helping someone else.