Ruby on Rails - Roo gem | Issue opening active record attachment

677 views Asked by At

Status:

  • I have a simple new Ruby On Rails App
  • I need to import some data from MS Excel in .xlsx format
  • I programmed an uploading to ActiveRecord as attachment via an attribute called 'excel'
  • I found a gem called Roo which should do the opening of the attachment
  • ...via: Roo::Excelx.open() command
  • Then executes the accessing part of the file

Issue: Roo::Excel.open() doesnt work for:

  1. Roo::Excel.open(excel)
  2. Roo::Excel.open(excel.attachment)

What is the correct command to execute to open the ActiveRecord::Attachment?

2

There are 2 answers

3
WALVISK On

how about do this?

ModelName.excel.open do |file|
 xlsx = Roo::Spreadsheet.open(file)
end
0
Julian On

Actually I got an answer outside of Stackoverflow which was this:

Roo::Spreadsheet.open(ActiveStorage::Blob.service.path_for(excel.key), extension: 'xlsx')

I loaded one row from the Excel so must change code but opening it seemed to have worked! :-)