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
.txt file to my root directory directly/public/tmp/lib/app/models/mymodel.rbTried 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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With