The Challenges of the Search

Celine Nicolas
3 min readMay 20, 2021

When completing my Portfolio Project Review for Rails, I was given a code challenge to create a search function to look up specific instances in the database. In Rails language, I was tasked to create a scope method to extract only one instance out of the multiple instances in my database.

My first impression of the challenge was pretty naive. I looked up various articles showing the basic way to set up a search form. The first step I had to do was input the method into my models. My application involves various K-pop groups so I would input it into my K-pop groups model.

In this method, I am first determining what should be returned if the user inputs a value, or instance, in the search bar. If there is nothing, then it should return all the instances of Kpopgroups. If yes, then the self.where method looks for an id that matches the id defined in the variable group, also known as the kpopgroup_id.

After inputting the method, I would also have to add in the search param in my strong params. Strong params are used as a security method to prevent assigned request params to object unless they are explicitly permitted. I also would have to call the method in my index action, since that is where the search option would be placed.

After completely the logicality of this function, it was now time to create the actual form in my views file. I would be using form_tag, since it would take in a path helper, a clean form for a URL, and enter in the search params.

At this point I was supposedly finished, but when testing out the function there would be no output. When I inputted a group in the search bar, it would just load back the list of groups. This means that I must have forgotten to input something in my views.

When looking back into my controllers and views, I realize that I set the instance @kpopgroups to the search function in my controller. Therefore, I had to input that instance somewhere in my index view, so the page would display all the groups. I would iterate through each instance (remember @kpopgroups is an array), and display the name of each group, as well as linking each name to the show page that displays information about each group.

However, the search function would still just display all the groups. After plugging in countless binding.pry’s in the controller and views, I realized the issue. Before inputting a group name in the search bar, the instance @kpopgroups is an array of the groups, along with all their attributes and ActiveRecord info. After entering a group name in the search bar and clicking enter, that array then has a value of one group, since we are only searching one group. I need to figure out a way to make sure the method outputs that one and only group but displays all the groups at default. This would require some explicit logicality.

First, I created an array in my search method in the Kpopgroup model. This array would start off empty, but would then push in a value of the searched group name and return that array, which would just be that group. If there is nothing being searched, then return all the groups.

The next thing I needed to do was refactor my view page. Since @kpopgroups is an array of all groups, I can create an “if” statement, outputting that one specific group when the array has a value of only one. If the array has more than one instance, then continue to display all the groups like usual.

After these two reactors, the search bar worked perfectly. It would display all the groups by default, then when searching a group would display that group name only. I think what really helped was making an array and inputting the value of the searched group inside that array. By doing so it would display the array as if it had multiple values, but having just that one value will display that one group name.

--

--