DEV Community

Karson Kalt
Karson Kalt

Posted on • Updated on

Using Query Parameters in Rails Controllers

In my recent project Houseplant Helper, I struggled to find a way to be able to call query params in a scalable way. Using the request object provided to us by rails and using the .send method, we can build a scalable private method that responds to all query params across the application.

Photo of URL with query parameters

Using the request ActionDispach::Request object given to us by rails inside of a controller action, we are able to quickly find any query parameters in the URI by calling the .query_parameters method which returns a hash with all query parameters.

Using an ||= inside of the .each method, we can conditionally define @plants if it has not been defined yet. From there, we can select only the plants from the query parameters using the .send method.

.send calls allows us to pass a string and call a method with the same name on an object. By calling .to_s, we can convert the return back to a string and compare against the value of the hash.

# ./controllers/plants_controller.rb

class PlantsController < ApplicationController

...

private

   def find_and_set_query_parameters(request)
        if !request.query_parameters.any?
            request.query_parameters.each do |scope, value|
                @plants = @plants.presence || @user.plants
                @plants = @plants.select do |plant|
                    plant.send("#{scope}").to_s == value
                end
            end
        else
            @plants = @user.plants
        end
    end

end
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
juanvqz profile image
Juan Vasquez • Edited

Suggestion, because I think it’s easier to read it.
if request.query_prameters.any?

And I think you can use this one too

@plants = @plants.presence || @user.plants
If plants is already defined it won’t consult the @user.plants again

Collapse
 
karsonkalt profile image
Karson Kalt

Thanks for the tip Juan, implemented and updated!

Collapse
 
juanvqz profile image
Juan Vasquez

thank you for share