Skip to content

Instantly share code, notes, and snippets.

@danielsousaio
Created November 18, 2017 11:07
Show Gist options
  • Save danielsousaio/bd0507d0473d879a02ee7db565ff3eb6 to your computer and use it in GitHub Desktop.
Save danielsousaio/bd0507d0473d879a02ee7db565ff3eb6 to your computer and use it in GitHub Desktop.
Filtering from Scratch
# 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