0

I am using axlsx_rails to produce reports in an application and I am attempting to write my first (ever) report using this gem. The report is to allow the user to select a specific horse for the report and to output the horse and it's corresponding foals to the same sheet. There is only one table (the Horse table) used for this purpose. A horse's foals are found by querying the same table by the horse ID to either the Dam_id (if a mare) or the Sire field (if the horse is a Stallion). For simplicity, since the queries are working fine, I have not included the code on determining whether a mare or a stallion.

I am successful in outputting the horse to the spreadsheet, but although the foals are generated in the controller (I have used debugger to check), they are not being recognized in the sheet output.

Relevant code includes the following:

  1. In the index_by_horse_report.html.erb, the horse is first selected and then:

        ....
    
      <%= link_to 'Download as horse_report.xlsx', index_by_horse_report_path(format: :xlsx ) %>
    
         ....
    
  2. In the controller:

    def index_by_horse_report
       @horses = Horse.find_by_sql ["SELECT horses.id.....WHERE horses.id = ? ", @horse_id]
       @horses.each do |horse|
            @foalings = Horse.find_by_sql ["SELECT horses.id...... WHERE horses.sire_id = ?", horse.id]
       end
       respond_to do |format|
          format.html
          format.xlsx {
          response.headers['Content-Disposition'] = 'attachment; filename="horse_report.xlsx"'
         }
       end
    end
    

The queries return 1 horse and 2 foals.

  1. In the index_by_horse_report.xlsx.axlsx:

      wb = xlsx_package.workbook
    
      wb.add_worksheet(name: "Horse Report") do |sheet|
      sheet.add_row ["NAME","REGISTRATION#1","REGISTRATION#2", "SIRE", "DAM", "FOALING DATE", "BREED", "Colour", "GENDER", "OWNER", "DATE ARRIVED", "DATE LEFT", "NOTES" ]
      @horses.each do |horse|
          sheet.add_row [horse.horse_name,horse.registration_number,horse.registration_number_2, horse.sire_name, horse.dam_name, horse.foaling_date, horse.breed_name, horse.colour_name, horse.gender_name, horse.owner, horse.arrival_date, horse.date_left, horse.notes ]
          sheet.add_row [""]
          sheet.add_row ["FOALINGS"]
          sheet.add_row ["NAME","REGISTRATION#1","REGISTRATION#2", "SIRE", "SIRE/DAM", "FOALING DATE", "BREED", "Colour", "GENDER", "OWNER", "NOTES" ]
          if @foulings
             @foulings.each do |foul|
                sheet.add_row[foul.horse_name, foul.registration_number....]   
             end
           else
              sheet.add_row ["NO FOALS"]    
           end
         end
       end
      end
    

The excel spreadsheet shows:

First row:the column headings, Second row: Information for the horse selected, Third row: empty Forth row: FOALINGS Fifth row: The foal headings Sixth row: "NO FOALS"

Any help that can be given to help me discover why the foal information is not printed will be very much appreciated.

Thanking you in advance,

user3331154
  • 177
  • 1
  • 11
  • Just a question, is your variable really @foalings and not @foulings? – noel May 26 '16 at 14:47
  • 1
    Plus, you might check for nil values in your foal row. Axlsx may not accept nil values for a cell. – noel May 26 '16 at 14:51

1 Answers1

0

Thank you Noel.

This was just plain stupid. It is foalings not foulings. Lesson learned: When doing something new, don't assume errors are due to a complex reason because of doing something new - check for the silly simple mistakes first!

user3331154
  • 177
  • 1
  • 11