What's new? | Help | Directory | Sign in
Google
                
Search
for
Updated Sep 30, 2008 by rogerpack2005
Labels: Featured
EnglishLikeQueries  
English like queries instructions and install directions

Introduction

This is a rails plugins allowing you to have nice, easy to read, easy to type statements than the traditional ActiveRecord syntax of

instances = School.find :all, :conditions => ["name LIKE '?%'", George']

Ex:

School.where 'name starts with' => 'George' # like an english sentence
People.where 'id in' => [35,36,37] # either id is 35, 36, or 37
People.where 'id in' => [35..37, 490, 499..588] # handles compound arrays and such nicely.
People.where 'name doesnt include' => ['fred']
person = People.first_where 'email address' => 'fred@fred.com'
person = People.fwhere 'email address' => 'fred@fred.com' # fwhere == first_where -- less typing
person.orders.where 'price >' => 6.00 # fred's orders over $6.
People.where 'name matches' => /t+tt$/ # handles ruby regexp's, as well as just text [which will be interpreted as a regex]
People.where 'date created > ' => Time.now # it adds in missing _'s if they are needed

Also allows for things like case insensitivity, by adding the word sensitive an s

People.where 'name includes' => ['george', 'park'] # defaults to insensitive
People.where 'name sensitive includes' => ['george', 'park'] # case sensitive
People.where 'name sincludes' => ['george', 'park'] # or abbreviate 'sensitive' as s

It is very addictive!

Details

install thus:

script/plugin install http://ruby-roger-useful-functions.googlecode.com/svn/trunk/rails/english_like_queries

from your rails app root directory then run script/console and test it out!

on windows:

ruby script\plugin install http://ruby-roger-useful-functions.googlecode.com/svn/trunk/rails/english_like_queries

Note that this is still alpha and probably will never be released, per se, but it still works like a charm, and be very very friendly. You may never want to use :conditions => ... again.

Its cousins, with lesser english like ability, are listed here.

Any feedback is welcome! Leave a comment at all!


Comment by rogerpack2005, Jun 04, 2008

works with rails 2 and mysql probably with others? you can also do

Lead.where 'name =>' => 'fred' # extra => acts as equals
Lead.where 'name =' => 'fred' # use an equals
Lead.where 'name is' => 'fred' # use the word is, like English
Lead.where 'name includes any' => ['fred', 'george'] # can include either fred or george
Lead.where 'name includes all' => ['fred', 'george']
# a dirty trick is you can still [shh] use symbols, though I recommend strings
Lead.where :name => 'fred' # works
Comment by rogerpack2005, Jun 04, 2008

you can also use 'gte' stuff, like Lead.where 'date_created gte' => Time.now

Comment by rogerpack2005, Jun 04, 2008

you can also use activerecord instance

Lead.all_where :school => school_instance
Comment by rogerpack2005, Jun 05, 2008
LeadStatus.where 'created_at >' => Time.today
Comment by rogerpack2005, Jun 06, 2008

version as of june 6 12:40 pm now includes

>> capella.ps.all_where :all
>> capella.ps.all_where 67 # searches for that id
>> capella.ps.all_where 68..70 # those id's
>> capella.ps.where :first # first
>> capella.ps.where 87..100 # first within those id's

similar to AR's find.

Comment by rogerpack2005, Jun 06, 2008

works with booleans, too

instance.programs.all_where('enabled' => true) # if that field is a boolean, rails converts it over nicely
Comment by Tomasz.Wegrzanowski, Aug 12, 2008

Is School.fwhere some other function or just typo of where? Typos in code are very confusing :-)

Comment by rogerpack2005, Aug 18, 2008

fixed it -- it's just a shortcut for first_where and confusing yes :)

Comment by rogerpack2005, Aug 18, 2008

Here's an example of 'case sensitive equals'

>> teach.state_programs.where 'state sensitive' => 'KY'
>> teach.state_programs.where 'state sensitive=' => 'KY'
>> teach.state_programs.where 'state s=' => 'KY'
or
>> teach.state_programs.where 'state sensitive equals' => 'KY'
#equals is always assumed
>> teach.state_programs.where 'state insensitive' => 'KY'
>> teach.state_programs.where 'state i' => 'KY'

all of those do the same thing.
Comment by rogerpack2005, Aug 18, 2008

Here's an example case sensitive regexs is the default?:

>> CampusLocation.where 'name sensitive matches' => /delaware/
>> CampusLocation.where 'name smatches' => /delaware/

Sign in to add a comment