Project

search_for

0.0
No commit activity in last 3 years
No release in over 3 years
This gem will add a chainable "search_for" method to your ActiveResource classes, allowingallowing you to pass in a search query and fields to search on.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 3.0.0.beta3
 Project Readme

Introduction¶ ↑

A simple gem for adding a chainable search method to ActiveRecord::Base (rails 3)

Installation¶ ↑

To install, add this to your bundler Gemfile

gem 'search_for'

Next, go to the root of your project in a shell and run this command (you may need to use “sudo” depending on your environment):

λ bundle install

Usage¶ ↑

Suppose you’d like to create a simple quicksearch form for searching your Book database. We’ll assume your books have the following attributes: “title, author, description, genre”

If someone searches for “romance”, we’d like to it to return any books where the title, author, description, or genre matches “romance”

If someone searches for “space odyssey”, we want it to narrow results: now a column attribute matches only if that attributes contains both “Space” and “Odyssey”.

How does it work? Assuming our form posts a ‘query’ param, then we could do something like this in our controller:

Book.search_for params[:query], :on => [:title, :author, :description, :genre]

The sql generated would look something like this (assuming params = “space odyseey”):

Book.search_for(params[:query], :on => [:title, :author, :description, :genre]).to_sql
# ==>
#    SELECT * FROM "books" WHERE (
#         (title LIKE '%space%' AND title LIKE '%odyssey%')
#      OR (author LIKE '%space%' AND author LIKE '%odyssey%')
#      OR (description LIKE '%space%' AND description LIKE '%odyssey%')
#      OR (genre LIKE '%space%' AND genre LIKE '%odyssey%')
#    )