RAILS 2.0 READY!
Version 1.2 - released Oct 30, 2007. ChangeLog
Overview
Do you have a query conditions that look like this?
“(name like ‘tim%’) and (age < 10) and (category=’friends’ or category=’family’)”
Do you need a way to easily build these queries, with the ability to add more or less conditions based on user input?
Enter [RailsWhere]
Installation
script/plugin install http://railswhere.googlecode.com/svn/tags/railswhere
Example Usage
Simple
User.find(:all, :conditions => Where{|w|
w.and "first_name like ?", "#{first_name}%" if params[:first_name]
w.and "last_name like ?", "#{last_name}%" if params[:last_name]
}Nested conditions
w = Where.new
for field in [:first_name, :last_name, :phone, :email]
w.and('#{field} like ?', params[:search][field] + '%') unless params[:search][field].blank?
end
w.and { |sw|
for status in params[search][:statuses].split(',')
sw.or 'status=?', status
end
}
User.find(:all, :conditions => w.to_s)
# '(first_name like 'tim%') and (last_name like 'harper%') and ( (status = 'new') or (status = 'expired'))'Negate an entire condition conditions
w = Where.new
w.and_not { |ww|
ww.or "x=1"
ww.or "x=2"
ww.or "x=3"
}
User.find(:all, :conditions => w.to_s)
# 'not ((x=1) or (x=2) or (x=3))'Combine 2 where clauses together
w = Where.new("id=5")
y = Where.new("first_name='tim'")
User.find(:all, :conditions => (w | y))
# '(id=5) OR (first_name='tim')'
User.find(:all, :conditions => (w & y))
# '(id=5) AND (first_name='tim')'Help
Need help? Have questions? Try:
Docs
Install the plugin, go to the plugin folder, run 'rdoc'
Author
Tim Harper ( 'tim_see_harperATgmail._see_om'.gsub('_see_', 'c').gsub('AT', '@') )
