Hello friends-
There is a new release of the ez-where plugin. This plugin makes it
easy to do complex ActiveRecord queries without writing any SQL. Ruby
operators are mapped to SQL operators like so:
foo == ‘bar’ #=> [“foo = ?”, ‘bar’]
foo =~ ‘%bar’ #=> [“foo LIKE ?”, ‘%bar’]
foo <=> (1…5) #=> [“foo BETWEEN ? AND ?”, 1, 5]
id === [1, 2, 3, 5, 8] #=> [“id IN(?)”, [1, 2, 3, 5, 8]]
<, >, >=, <= et all will just work like you expect.
So you can use blocks and ruby ops for very complex conditions. Here
is a teaser:
find all articles with title, body or extended LIKE "%#{params
[:search]}%"
AND (author.name = params[:author] OR comment.body LIKE "%#{params
[:search]}%")
@articles = Article.find_where(:all, :include => [:author,
{ :comments => :users }]) do
|article, author, comment|
article.any_of(:title, :body, :extended) =~ “%#{params[:search]}%”
any {
author.name == params[:author]
comment.body =~ “%#{params[:search]}%”
}
end
=>[“(articles.title LIKE ? OR articles.body LIKE ?
OR articles.extended LIKE ?) AND ((authors.name = ?)
OR (comments.body LIKE ?))”, “%foo%”, “%foo%”, “%foo%”, “Ezra”, “%
foo%”]
More examples on my blog:
http://brainspl.at/articles/2006/06/30/new-release-of-ez_where-plugin
Get it here:
$ script/plugin install svn://rubyforge.org//var/svn/ez-where
Mailing list here:
http://rubyforge.org/mailman/listinfo/ez-where-devel
Cheers-
-Ezra