Image of Lucian Ghinda writing for notes.ghinda.com
December 14th, 2024

Consider carefully when deciding to create an array column in Rails

Think carefully about whether I truly need an array column in Rails or if it's more effective to create it as JSON from the start.

Sooner rather than later, I would like to enrich that column and add some extra information. 

Which will create the need for a 3-stage migration:

  • Add new column while keeping the old one 
  • Execute data migration by generating the new structure in the new column
  • Change the code to use the new column
  • Drop the original column

Before deciding, I should clarify the reasons this will always be an array instead of just assuming it is one. And if there are no good reasons for it maybe it should be a JSON/JSONB column from the start. 

Of course, could be that this should just be a nested model -> but that is a decision to be discussed in another post :)