Chaining queries with Ruby

Stephen Rivera
Nov 7 · 4 min read

I had decided to create a simple e-commerce website for a donut shop as a project for my coding bootcamp. Everything was going well and work was being done at a good pace. I was able to display items nicely in a neat list, redirect a customer to different areas of the site as needed and have full CRUD and everything that is expected of the project. However one issue that I had come across: I wanted users to be able to sort through products. This simple task proved to be more puzzling than I expected though.

The Problem:

I had a model called Products which looks like this in the database:

Products table

The products page can already be divided between Donuts and Beverages. Both are subclasses of the Product superclass. This raises an issue for what I want to do though. In order to render only Donuts or only Beverages, I had to query for them in my controller, like this:

Single query for “Donuts”

This was in essence sorting the products by the attribute of `type`. I wasn’t able to make an additional query though if a query was being done for Product type. This all needed to be done without the help of JavaScript.

Perhaps you are asking yourself, what is a query and why does that effect my ability to sort a list with Ruby? A query is simply a request for data from a database. The request relies on cookies and as you may have noticed uses params. The params take the information that is stored in the button, and enters it into the query.

When I make a query on this site, I’m simply asking the database to only send the Products that I’m specifically asking for, so they can then be rendered on to the page. For example, because I had set up my Products to have a `type` attribute in my database, I was able to easily query for a specific type of `Product`, as shown in the example above. The issue arises when you want to add another query to this initial query of `Product` type. For this project, I want to allow the user to sort Products by price or by the average user rating of the Product in addition to the type of Product. Unfortunately, I wasn’t able to perform the same query for the additional sorting methods I wanted.

Methods used for sorting Products.

It took a bit of thinking but a solution was eventually found.

The Solution:

As is often the case when coding, my problem was solved more easily than I originally figured. I found that with queries, the word “query” acts as a place holder. Think of it like writing a method that takes an argument. When you write a method with an argument, there needs to be a place holder where the argument would be called. What you name this placeholder doesn’t really matter as long as you remember to refer to this name when you want to use this argument in your function. This case is similar for writing queries. As long as you refer to this specific query it will perform this specific query allowing you to chain together multiple queries. This could be done through if then statements, like so:

Products controller

So now when I click on a certain type of Product and then narrow my search with a `sort` query, the two queries will be performed simultaneously. I’m sure you can imagine how tedious this can be as you add more levels of sorting. Unfortunately, this is the easiest way to sort in Ruby, to my knowledge, without the aid of JavaScript.

Hopefully this guide is helpful if you find yourself needing to sort items on a Ruby on Rails website. There are multiple ways to implement this feature, feel free to experiment to find what works best for your project.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade