Enum thoughts

Jr Medina
2 min readMay 31, 2021

I have been doing some freelance work for about a month now, and have been catapulted into learning things I had no idea existed in the world of Rails. It has been an awesome journey and the more I learn the more I realize that I don't know what I don't know.

From what little I have dealt with Enums, my understanding is that this a way to manage the state of an instance once it is created. As a quick example, let us say there is a conversation model in your Rails application. It will require two users and a conversing attribute. For this example, the users don't really play a factor, what we are looking at is the conversing attribute.

Upon a conversation being created with the two users, the conversing attribute will be one of two things “active” or “inactive”. An easy way to get “inactive” as the initial state, is to use an Enum within the class. This can be set as an array or an object; from my reading, it seems as the best convention is to set it as an object.

This will come through as the integer you set as the value to the key in the model table, which I set to “0” and the key is “inactive”. This can be changed in an update to be “active” or “1” in the table.

My understanding is that this takes up less space, and helps keep order by only storing integers in your table, and allowing an attribute to only to be set to specific, predetermined options. As of now, this is a rough understanding of how Enums work within Active Record. I will follow up with coded examples using Enums after some more experimentation.

--

--

Jr Medina

Soon-to-be Flatiron School Successor, with an enthusiasm towards Front-End Web Development, lifelong learning, and continuous improvement.