Skip to content

Instantly share code, notes, and snippets.

@thegrubbsian
Created February 11, 2012 19:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thegrubbsian/1803578 to your computer and use it in GitHub Desktop.
Save thegrubbsian/1803578 to your computer and use it in GitHub Desktop.
QueryBuilder Example
class Product < ActiveRecord::Base
def self.find_by_criteria(params)
query = where("id IS NOT null") # A little odd but not sure how to get an ActiveRecord::Relation that returns "all"
query = query.where("price = ?", params[:price]) if params[:price].present?
query = query.where("manufacturer = ?", params[:manufacturer]) if params[:manufacturer].present?
query = query.where("retailer = ?", params[:retailer]) if params[:retailer].present?
query = query.where("category = ?", params[:category]) if params[:category].present?
query
end
end
@koriroys
Copy link

This is what our search ended up looking like: https://gist.github.com/1810085

thoughts on a better way? I'm sure there is...

Lines 11 and 12 were pretty straightforward following your example. It got ugly after that, when we wanted to find the games that matched the ingredients that were checked.

Line 28 is a postgres monstrosity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment