Arrays and Hash Attributes in Rails Domain Models
A quick and dirty way to send attribute data as hashes or arrays from your backend rails API to your frontend.
Say you want a model in rails that stores a hash and array. You’re in luck! There is a simple, pain-free way to make this happen. The example I am going to reference below is for a rails app that is API-only (no views). If you are using rails for full-fledged MVC, just take this into consideration.
Let’s begin by generating your model. We’ll use rails -g resource as the terminal command, since we want empty shells for our model and controller, our flushed out migration file and our routes. Remember this an API only, so we don’t care about the view file. Let’s create a user with the following attributes: first name (string), birthdate (datetime), email (string), password (string), bio (text), gender (string), interested-in-gender (array of strings), interested-in-age(hash with integer values). Currently, I am building a dating app for vegetarians, hence why some of my fields are what they are. Regardless, I need to generate an attribute (interested-in-gender) that contains an array of strings and an attribute (interested-in-age) that contains a hash with integer values. When we run the terminal command it will look something like this:
When generating a resource within a terminal command, you do not explicit need to denote string attributes, as rails will give the attributes that default designation. Which means that our interested_in_age and interested_in_gender attributes with be strings. This may seem confusing, especially because our interested_in_age has will contain values that are integers, but this is the way that it’s done.
Once you’ve migrated your tables, your schema should reflect these fields as strings. Now the essential part. Within your user model you want to enter the following to serialize your interested_in_gender and interested_in_age fields as an array and hash, respectively:
And it’s literally that simple. A quick and dirty way to now serialize your data and push it to the frontend as an array and/or hash. To give you a sense of what this looks like, check out my seed data for a user instance:
Once my data is seeded, the interested_in_age has will contain keys (max_age, min_age) that have integer values and the interested_in_gender will contain an array of strings. Because this is a quick and dirty way to do this, you need to be extremely careful when posting or patching instances from the frontend, making sure that you use the same fields. There are few checks in place, besides the fact that your backend is now expecting a hash and array for those two fields (which for now is enough data validation for me). For example, it would get pretty nasty if you were querying information about your users that are interested in women and you have instances that contain arrays with ‘Women’ and ‘women’. I know you can write some quick logic to downcase the values, but who wants to have to anticipate inconsistent data conventions when you are pulling from the backend if it can be avoided.
Ruby is considered the lazy programmers language and this is a lazy way to store data attributes as arrays and hashes. If you are creating a sophisticated app that is meant to scale, perhaps it would be a better idea to create additional data models for interest or some other configuration that doesn’t require to just serialize your data in a different format. For the purposes of my small application, I’m just going to be lazy.