Using model.find

I need to select rows form a table based on the first letter of the name
column. Like a teledex there will be a letter provided by a request.
Currently i have

    @artists = Artist.find(:all,
    :conditions => [ 'LOWER(name) LIKE ?',
    '%' + params[:id].downcase + '%' ],
    :order => 'name ASC',
    :limit => 8)

However this returns rows that have the letter anywhere in the name
value. I need only rows that have the first letter of their value.

Thanks for any help

thats because you are using %[letter]%

% is a wildcard and so you are asking the db to return any row where
[letter] appears in the name column.

if you want any last name that starts with [letter], use ‘[letter]%’

Chris

On Monday 04 December 2006 01:17, Stewart wrote:

However this returns rows that have the letter anywhere in the name
value. I need only rows that have the first letter of their value.

     params[:name].downcase + '%' ],

Remove the first ‘%’, it’s a placeholder for an arbitrary string. I take
it, params[:id] was not what you intended to use.

Also, bear in mind that if you have a database index on the name column
it will most likely not be consulted as names are verbatim in the
index, not in lowercase. To make use of an index in a case like this,
the DBMS has to support functional indexes. MySQL apparently is too
dumb for it, but PostgreSQL has this feature.

Michael


Michael S.
mailto:[email protected]
http://www.schuerig.de/michael/