I am using this file upload example to upload some files in the database. I have a rruby.rb with r-script in it which makes statistical analysis of the file(name it A) , and as a result it saves a plot in the directory where the file A is situated.
What I need is to extract the file from the database, do analysis and save the plot back into a database.
Where should I place the code of rruby.rb?In uploads_controller, upload.rb?
I am using this file upload example to upload some files in the database.
Share
I would keep the
rruby.rbfile separate and use it as is. But where to place that file?There are two equally valid options. Either you place it in
libfolder, or place it in themodelsfolder.I would place it in the
libfolder if I consider more generic and code that I happen to need. Code inlibis code that eventually could be turned into a gem. It is code that I consider completely unrelated to any business code or models.If the code is strongly related to my business domain and other models.
So in your case I would place the
rrubyfile in thelibfolder since (if I understand correctly) it contains an pretty isolated procedure, no dependency on the rest of the code, just needs a file as input, and will generate a file as output.Another advantage of keeping
rrubycode separate is that you can test it in isolation as well.Hope this helps.
[EDIT: added example implementation]
I presume your
rrubyis a simple script, so you wrap it in a module, since it performs statistical analysis I call itStatistical, but you should give it a more appropriate nameSo in
lib/statistical.rbwriteThen in your uploadscontroller or uploads model, you do something like
Normally all files inside the
libare automatically added to rails load-path, so this is all you need to do.