Rails where plugin

Plugin details

A plugin that has the ability to add more or less conditions based on user input.

Websitehttp://code.google.com/p/railswhere/ Repositoryhttp://railswhere.googlecode.com/svn/tags/railswhere Author Tim Harper Tags Search LicenseMIT

Documentation

Install the plugin:
ruby script/plugin install http://railswhere.googlecode.com/svn/tags/railswhere

== Returning SQL

sql = Where.new('x=?',5).and( Where.new('x=?',6).or('x=?',7)).to_s
 # returns (x=5) and ( ( x=6 ) or ( x=7 ) )



== Building a complicated where clause made easy

def get_search_query_string
 
   where = Where.new
   where.and('users.first_name like ?', params[:search_first_name] + '%') unless params[:search_first_name].blank?
   where.and('users.last_name like ?', params[:search_last_name] + '%') unless params[:search_last_name].blank?
 
   status_where = Where.new
   for status in params[search_statuses].split(',')
     status_where.or 'status=?', status
   end
   where.and status_where unless status_where.blank?
 
   where.to_s
end



User.find(:all, :conditions => get_search_query_string)



=== Inline

User.find(:all, :conditions => Where.new('first_name like ?', 'Tim').and('last_name like ?', 'Harper') )

Further Documentation

There is currently no advanced documentation for this plugin.

New documentation

Edit plugin | (0 older versions) | Last edited by: Guest, 8 months ago