# \[ANN\] ActiveRecord English Like Queries v0.0.2

**URL:** <https://www.ruby-forum.com/t/ann-activerecord-english-like-queries-v0-0-2/146227>\
**Category:** Ruby\
**Created:** [August 19, 2008, 9:12am UTC](https://www.ruby-forum.com/t/ann-activerecord-english-like-queries-v0-0-2/146227 "2008-08-19T09:12:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [August 19, 2008, 9:12am UTC](https://www.ruby-forum.com/t/ann-activerecord-english-like-queries-v0-0-2/146227/1 "2008-08-19T09:12:41Z")

</div>

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](http://code.google.com/p/ruby-roger-useful-functions/wiki/EnglishLikeQueries)

-=R

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [August 19, 2008, 10:37am UTC](https://www.ruby-forum.com/t/ann-activerecord-english-like-queries-v0-0-2/146227/2 "2008-08-19T10:37:59Z")

</div>

Roger P. wrote:

> Typically AR basically stinks to type in–it is way too wordy, and, for  
> Currently as a rails plugin.  
> Check it out.
> 
> [Google Code Archive - Long-term storage for Google Code Project Hosting.](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

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [August 19, 2008, 7:24pm UTC](https://www.ruby-forum.com/t/ann-activerecord-english-like-queries-v0-0-2/146227/3 "2008-08-19T19:24:42Z")

</div>

Michael M. wrote:

> Roger P. wrote:

> > 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’]

I see that as a plus, not a minus. SQL is well-known, so if you need to  
know how to do something there a many examples and resources.

> > 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.

Pain of SQL? Anyone writing code, and writing code that interacts with  
a database, owes it to themselves (and their clients if they are coding  
for $) to learn at least basic SQL and know where to look to learn more.  
It’s a DSL meant for relational data. For the most part it is not  
hard, and it gives you the power to write compact, efficient queries.

> as doing something like where :title =\> [:starts\_with, ‘Something’].  
> That would require a lot less code and zero parsing.

Attempts to allow code that is “English-like” tend to come off as the  
worst of both worlds. You lose the compactness and precision of code,  
while never really getting the fluid expressiveness of natural language.  
It tends to look and read as clunky.

I’d much rather have code that reads like Ruby but allows for raw SQL  
when needed (Sequel, as a prime example, or Og), rather then code that  
doesn’t quite read as code, and doesn’t quite read as English either.

And if you’re going to write DB apps, then geek up and just learn the  
SQL.

–  
James B.

[www.happycamperstudios.com](http://www.happycamperstudios.com) - Wicked Cool Coding  
[www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys  
[www.ruby-doc.org](http://www.ruby-doc.org) - Ruby Help & Documentation  
[www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [August 19, 2008, 10:13pm UTC](https://www.ruby-forum.com/t/ann-activerecord-english-like-queries-v0-0-2/146227/4 "2008-08-19T22:13:14Z")

</div>

> 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
