How do I check that the value of a field against the legal values in a file?

What if you were writing an online store, selling T-shirts, for your client and wanted them to be able to add in new stock?

You could add in a model for shirts and let the store administrators add in new items via the admin section. But in order to save time and reduce errors, you need to make sure that the colour selected is in the list sent through by the supplier. And if it isn’t, show an error so that the administrator knows that they’ve chosen wrongly.

Rails offers an easy way to do this – the “inclusion validation”.

Your shirt model will look something like this:

In other words, we’re saying that you need to supply a colour and it has to be either green, blue or red.

But the supplier changes their colours every few months. And it comes in a text file, not as ruby code.

How do I change the %w{green blue red} array of colours to be something loaded from a file?

(Slight aside – %w{this that} is a shortcut for [“this”, “that”] – in other words %w{ } builds an array of strings for you, using spaces as separators between each individual item)

First thing you need to do is find a place to store this list. Currently, it’s in our source code, instead we need a variable to store it in.

When you run the rails new my_app command, you give your application a name. And Rails builds an object representing that application, called MyApp::Application. It lives in config/application.rb. And this application object has a property called “config” that lets you store arbitrary configuration values in it.

This looks like the perfect place to store values like our colours; something that changes infrequently, but we don’t want stored in our model’s source code.

So somewhere we would have this line of code:

and our model becomes

Where does this configuration line live?

Rails has a folder called config/initializers – any ruby file in this folder is loaded and evaluated as the application starts.

So we add in a new file – config/initializers/colour_values.rb – and put the line of code in there.

But of course, this is still a static list. It’s not the file that our suppliers are giving us.

Let’s grab that file from the supplier, and stick it in our config folder – so it lives alongside database.yml and secrets.yml and other configuration data.

Then we change our initialiser so that it finds the file, reads it into memory and then stores its details into our application’s configuration.

We figure out the filename of our colour values file. Then we open the file, read its contents, and then split it into an array; using the line-ending \n as a separator. Finally we store the values in our application config.

So there you have it.

  • The inclusion validation makes sure your value is included in the list that you supply
  • You can store values that make up your application’s configuration in the Application config property
  • Rails gives you the config/initializers folder specifically for setting up before the rest of the application starts
  • You can use then an initialiser to read data from a file on startup