Skip to content

a simple ruby on rails plugin to add scoped search to ActiveRecord

License

Notifications You must be signed in to change notification settings

nicksellen/search_scope

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SearchScope
===========

A simple extension to ActiveRecord to allow scoped search. You can define a number search types that will use different selection of fields.

I wouldn't want to use this to build my google killer search engine on but might be useful for either:

* small apps / infrequently searched models
* placeholder search funtionality until you move up to proper fulltext search like hyperestraier, ferret, solr, sphinx etc... 

Example
=======

class Hangup < ActiveRecord::Base

 search_scope :default => :everything, 
    :types => { 
      :everything     => [:inherited_from,:most_annoys,:name,:description],
      :related_people => [:inherited_from,:most_annoys], 
      :name           => [:name]
     }

end

# Any hangups containing the text cleanliness
Hangup.search 'cleanliness'

# Search multiple terms  - careful as you'll generate (number of terms) * (number of fields) SQL LIKE clauses
Hangup.search %w[stickers feathers]

# Any hangups that were either inherited from or annoy someone called mike
Hangup.search 'mike', :type => :related_people

# The above query will result in the following SQL being generated
SELECT * FROM `hangups` WHERE ((lower(`hangups`.`inherited_from`) like '%mike%' OR lower(`hangups`.`most_annoys`) like '%mike%'))

# Internally this uses named_scope so you can do all the cool stuff that allows
Hangup.search('teeth').find(:first, :order => :severity)

Copyright (c) 2009 Nick Sellen, released under the MIT license

About

a simple ruby on rails plugin to add scoped search to ActiveRecord

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages