Skip to content

rayvinly/searchable_attributes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SearchableAttributes
====================

  Searchable plugin is a Rails plugin that makes searching on ActiveRecord
  attributes a snap.  It makes use of chained named_scope's so attributes
  can be optional in a search.  It also generates only one SQL query so it
  does not slow down your database.

  This is not about full-text search.


Example
=======

  You want to perform search on the User model:

    class User < ActiveRecord::Base
      searchable_attributes
    end

  When you do a search:

    def index
      @users = User.with_first_name_equal('Gilbert').with_last_name_equal_if_not_null('Arenas').with_address_like('Main Street').with_age_greater_than(25).with_state_equal(['VA', 'MD', 'DC']).with_position_end_with('Guard').with_salary_within_inclusive((100..10000)).with_injured_equal(true).with_join_date_greater_than_or_equal_to(5.years.ago)
    end

  Note that the _equal named_scope takes an array and uses IN (val 1, val_2).

  The beauty of it is you can pass the search params straight through:

  def index
    @users = User.with_first_name(params[:user][:first_name]).with_injured(params[:user][:injured])
  end


Copyright (c) 2009 Raymond Law, David Pranata, and Intridea, Inc., released under the MIT license

About

Searchable plugin is a Rails plugin that makes searching on ActiveRecord attributes a snap

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages