Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to store text files in a Ruby on Rails directory?

I have a text file in my Ruby on Rails application that I would like to call in a model. The problem is, I want to follow rails conventions in terms of placing files.

The reason I'm using a .txt file is because I have a very large array of words I have to iterate over. This is for a model validation and I need to ensure that the input does not contain any of these words.

I could just declare an array, but it would make my models to fat. Therefore, I'd like to read from a file where I place my comma separated array elements.

There are several ideas I have

  • Root Directory: I could add the .txt file to my root directory directly
  • Public Directory: Could store it in /public
  • Temp Directory: Could store it in /tmp
  • Lib Directory: Could store it in /lib
  • Inside Model: Could just store it inside /app/models/mymodel.rb

Tried to find a similar answer here, but couldn't find a consensus. Anyways, this is a two part question. How would I read from the file inside the model. Would I do something like

file = File.read("#{Rails.root}/public/textfile.txt")

Thanks for your help

like image 509
Richard Hamilton Avatar asked Jan 02 '26 04:01

Richard Hamilton


1 Answers

There are no conventions for storing files. All files can be stored in the public folder. This can vary if you're using Heroku or hostings that do not allow write access on the file system, and obviously you need to write.

Otherwise, you must take into account what is the purpose of this file. It might be a better idea to store files in a database or using rails store systems that are explained here: session store guides and here you can find more information about security

like image 174
ppascualv Avatar answered Jan 04 '26 03:01

ppascualv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!