From: Roger Pack Date: 2008-08-20T05:09:44+09:00 Subject: Re: ActiveRecord English Like Queries v0.0.2 > 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. Interesting. You can also use named scopes to accomplish something similar albeit a more hard coded approach. > 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. I like what you described. Basically I like anything that avoids the tedious :conditions => [] and that reads well, so...sure. > 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 Yeah named scopes are another way of doing it, as well as hard coded functions like this. However, using english_like_queries you never have to build those methods, or new ones when you find that what you have isn't exactly right. It works in conjunction with named scopes, too--i.e. Post.enabled.where 'name starts with' => 'fred' What I really love is that you can use it on existing AR sets, a la user = User.find :first users.posts.where 'date_created < ' => Time.now.yesterday Somehow I find it quite useful--it is faster and [for me] more readable. YMMV. -=R -- Posted via http://www.ruby-forum.com/.