Created
November 18, 2017 11:07
-
-
Save danielsousaio/bd0507d0473d879a02ee7db565ff3eb6 to your computer and use it in GitHub Desktop.
Filtering from Scratch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This can be improved by Ruby Mixins in the long run, but this is the basics overview | |
class Grouping < ActiveRecord::Base | |
# Takes a gender and queries based on it | |
scope :gender, -> (gender) { where: ... } | |
end | |
class GroupingsController < ApplicationController | |
def index | |
@groupings = Grouping.all | |
filter_params.each do |key, value| | |
# In ruby send is the way to call a method, in this case gender method/scope | |
@groupings = @groupings.public_send(key, value) if key.present? | |
end | |
end | |
private | |
def filter_params | |
params.slice(:gender) # One can add more params here | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment