Created
February 11, 2012 19:00
-
-
Save thegrubbsian/1803578 to your computer and use it in GitHub Desktop.
QueryBuilder Example
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.