What is the best practice for handling mostly static data that I want to use in all my Rails environments?

Say, for example, I am running a Rails application that has static content that is relevant to all my environments, but I still want to be able to change if necessary. Examples: statuses, quiz questions, wine varieties, etc. There is a relationship between your user-generated content and this static data, and I want to be able to modify it if necessary, so it should be stored in a database.

I have always dealt with migrations to synchronize my team and all my environments.

I had people who dogmatically say that migration should only be for structural changes in the database. I see the point.

My counterargument is that this mainly β€œstatic” data is necessary for the application to function, and if I do not update it automatically (everyone is already trained to start the migration), someone will have crashes and search around what problem before they find out that a new required field has been added to the table and that they need to import something. So I just do it during the migration process. It also simplifies and simplifies deployment.

The way I specifically did this is to update my test fixture files with good data (which has a side effect, allowing me to write more realistic tests) and re-import it when necessary. I do this with connection.execute "some SQL" and not with models, because I found that Model.reset_column_information + a bunch of Model.create sometimes works if everything is updated immediately, but eventually explode on my face when I will push a few weeks later because I would have newer model checks that would contradict the 2-week carryover.

Anyway, I think this YAML + SQL process works to explode a little less, but I also find it pretty kludgey. I was wondering how people manage such data. Are there any other tricks in Rails? Are there gems for managing static data?

+4
source share
1 answer

In the application I'm working with, we use a concept that we call DictionaryTerms, which work like search values. Each term has a category to which it belongs. In our case, these are demographic terms (hence the data in the screenshot) and include terms related to gender, race, and location (e.g., state), among others.

enter image description here

Then you can use typical CRUD actions to add / remove / edit dictionary terms. If you need to transfer terms between environments, you can write a rake task to export / import data from one database to another through a CSV file.

If you do not want to import / export, you may want to place this data separately from the application itself, accessible through something like a JSON request, and ask the application to cancel the conditions of this request. This seems like a lot of work if your case is simple.

0
source

All Articles