From: Michael Morin Date: 2008-08-19T17:34:11+09:00 Subject: Re: [ANN] ActiveRecord English Like Queries v0.0.2 Roger Pack wrote: > I am pleased to announce the release of English Like Queries [pronounced > in a proper British accent] v0.0.2 > > Changes: > klass.where 'name includes' => ['abc', 'def'] now defaults to a .find > :all query--much nicer. > > > English Like Queries: > > Typically AR basically stinks to type in--it is way too wordy, and, for > lack of a better term, SQL like. > klass.find :all, :conditions => ["name LIKE '%?'", 'fred'] > > This library condenses that to be a little more englishy, by allowing > hashed parameters that are parsed for meaning, ex: > > klass.where 'name starts with' => 'fred' > > This reads better, is much faster, has support for regex, case > sensitivity, etc. without the pain of SQL. > Currently as a rails plugin. > Check it out. > > http://code.google.com/p/ruby-roger-useful-functions/wiki/EnglishLikeQueries > > -=R That's interesting, but I have to question the code:payoff ratio. Most ActiveRecord usage doesn't have particularly diverse queries. These queries can be easily and simply hidden away in single-line class methods to give them meaningful mnemonics like "name_starts_with". Using class methods is transparent, trivial and not limited by the features of your library. English is also a very subjective language. What you've done is limited users to a tiny, strict subset of English which is more or less the same as doing something like where :title => [ :starts_with, 'Something' ]. That would require a lot less code and zero parsing. But all of this replaces something you can do in one line anyway. Why add all that complexity? class Post < ActiveRecord::Base def self.title_starts_with(s) find :all, :conditions => [ "title LIKE ?", "#{s}%" ] end end -- Michael Morin Guide to Ruby http://ruby.about.com/ Become an About.com Guide: beaguide.about.com About.com is part of the New York Times Company