Skip to content

Instantly share code, notes, and snippets.

@morshedalam
Created April 18, 2015 06:02
Show Gist options
  • Save morshedalam/e3abb94594bb144c68b6 to your computer and use it in GitHub Desktop.
Save morshedalam/e3abb94594bb144c68b6 to your computer and use it in GitHub Desktop.
Filter model by dynamic scope using fields
module Filterable
extend ActiveSupport::Concern
module ClassMethods
def filter(filtering_params = [])
results = self.where(nil)
filtering_params.each do |key, value|
results = results.public_send(key, value) if value.present?
end if filtering_params.any?
results
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment