Saving an array to a Rails data base (the dirty way)

Tim Rinkerman
3 min readOct 23, 2020

--

So I might begin with saying I realize this might not be the most efficient or “proper” way to save an array of information to a back end. I have read there are ways to do this with a serializer, but being that I was in a time crunch to get a program to work this seemed to be the fastest way to achieve my desired outcome based on the knowledge I currently had.

For my example, I was trying to find recipes based on a collection of ingredients I currently have. However, if you’re anything like myself, when you’re trying to figure out a meal based on whats left in the refrigerator you most likely only have a jar of pickles, some eggs and the remnants of the case of beer from the night before. So naturally, that leaves a lot to be desired for a good recipe. So what if you see something you want to make but don’t have all the ingredients? It’d probably be convenient to be able to create a shopping list of what you’re missing huh? So that’s exactly what I set out to do.

Unfortunately this proved to be easier said than done. Thanks to the spoonacular API I was able to create a search function comparing a list of things I had declared I already had and it would return recipes that I could make with the current ingredients I had, or in most cases things I was only a few ingredients shy of being able to make.

The returning set of information was an object containing two arrays. One of the ingredients I had on hand as well as another of the missing ingredients to fulfill the recipe. When trying to compile a shopping a list I needed to save the list of ingredients to my back end. At first I tried sending back each ingredient individually with a post request but this proved to be too much for the server and would cause my program to crash if I was trying to pass more than one or two ingredients back.

After some research I had found a couple of different ways I could have taken care of this in a more concise, practical and quite frankly professional way, but it was the last thing I needed for my program to work correctly and the due date was the next morning.

What I ended up doing was passing the array as a whole for my argument within the post request and just allowed json to stringify it.

Naturally on the front end this provided a pretty grotesque looking bit of information. From there I took that string, broke it into an array, and then filtered out all of the unwanted characters before rejoining the array into a string. Originally looked something like this

Again I know this isn’t the most practical way to do this, and in fact there is even a better way to go about filtering the string result I get back from rails.

I am writing this blog as more of a message that there is more than one way to do things. I’m sure there is a keyboard warrior out there who would tear this apart. However, I couldn’t find a solution to my question of “how can I get this array from the front end to the back end and back to the front end” maybe I wasn’t asking the right question. But maybe some one else is asking the same question and needs this answer because it’s the last night before the project is due.

This was one of the biggest speed bumps in my final project at Flatiron and while I’ve said there is better ways to do this, I think it also demonstrates the nuance of what can be so great about building computer programs. I’ve only been programming for a couple of months now, and I built an app that at the beginning of the program I thought “wouldn’t it be cool if someone could make that app” and then I was able to make a working version of that app in just three weeks. Is it perfect? As explained by this very post, absolutely not. It does however serve as an example that really anything is possible and that you too can do what you might think is too far fetched.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response