Creating a Rails Api Query Param

Image for post
Image for post
my weekend spent debugging rails api

This weekend I decided to build a single page web app in vanilla javascript using a rails api backend. Everything was going great until I had to decide how to provide the user with searched data. See, after I fetched the data, I realized that the user may want filtered results. Hmmmmmm

I had a few options, I could write some higher order functions in javascript or I could figure out how my rails api could solve the problem. After a few minutes of thought, I remembered that I could use Active Record in rails to manipulate my data quickly, but this would happen prior to the user’s search. So I thought about maybe, making another query request each time the user filtered the data. Both solutions would work, but I thought they would create more code than necessary or I would have to make many requests to the server. I wanted a solution, that would be scalable and reduce the amount of code I needed to write. Personally, I just like my code to be tight and human readable as well as DRY.

Through research, I came across a solution that required me to use search query params. Great, I thought. Pretty simple. I understood the concept and more or less I was familiar with the tools necessary to accomplish the task. But wait…. How do you do it? As a beginner, the learning curve is steep, sometimes, I know things I should not know yet. Other times, I do not know things that are fundamental to programming and I should know. I think this is one of those things. I spent literally, four days trying to figure this out.

This blog was written with two goals in mind. First, I wanted to document what I learned over this weekend of acquiring knowledge via debugging, researching and troubleshooting. Finally, I wanted to pass along what I learned, it was hard to research this particular area, either the information was to advanced or not fundamental enough.

Let’s begin, on how to create a search query param in rails. There are many ways to create a search query param, this is just the way that I choose.

In order to create the param, I decided to use scopes. As described by Justin Weiss, a scope is just a way to grab data out of your database. Scopes are just a syntactic sugar for class methods in Active Record.

Based on the above example, I had a class Helpwanted that list jobs available. A user could filter the results, by industry and there are 6 industries. So I wrote scope methods query the results of Helpwanted and search by industry.

Next I want to connect the scope to the URI resources. So I will have to go to the config/routes.rb and connect the resources. This will look like …

In the terminal, I will run the rake routes | grep helpwanteds and I should see..

These are the URI’s that I need to fetch the data based on the user search. In order to test the URI route. I need to go to the controller model of Helpwanteds. In the controller I need to create an action for the route created. The controller.rb file should resemble the file below.

Image for post
Image for post

Once the controller actions are created, you can go to Postman and test the api routes, to ensure that they are working.

This route should show a list of all the helpwanteds with the industry of professional. Therefore, when a user queries the frontend, the resulting data will have a list of industry specific, helpwanteds.

After the entire weekend or searching, I was finally able to get minimally what I was trying to attain. I definitely think there are more efficient solutions, but as a beginner, I understand this one well for now. Feel free to comment or provide feedback.

Written by

Business Tech Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store