Find not working

Hi all,

I have this ruby script that simple rips through a CSV file, retrieves a
username and then search my database for that user and any associated
data. My problem is that the query I’m using to find my users isn’t
working even though this is pretty basic stuff that I am trying to do.
My query looks a little something like this:

@finduser = User.find(:first, :conditions => [‘upper(username) = ?’,
x[11].to_s.upcase])

x[11] is the field in my csv file that has the username, other then
that this is a simple script, but the user is never being found even
though ActiveRecord has the correct host and adapter information. My
question is there anyway to output the query string from my object or
the query that is being built? The only thing I can think of is that the
query is not being built correctly since for some of my test users the
correct username is there and should be found. Thanks in advance and
happy holidays,

-S

On Dec 27, 11:30am, Shandy N. [email protected] wrote:


Posted viahttp://www.ruby-forum.com/.

This is not a Rails forum.

On Mon, Dec 27, 2010 at 9:30 AM, Shandy N. [email protected]
wrote:

My query looks a little something like this:

@finduser = User.find(:first, :conditions => [‘upper(username) = ?’,
x[11].to_s.upcase])

x[11] is the field in my csv file that has the username, other then
that this is a simple script, but the user is never being found even
though ActiveRecord has the correct host and adapter information. My
question is there anyway to output the query string from my object or
the query that is being built?

Does the DB you’re using have a log? (MySQL does, so you can see
the queries generated directly.)

You could also use print statements or a debugger to confirm that the
value of x[11].to_s.upcase is what you think it is.

Hassan S. wrote in post #970921:

Does the DB you’re using have a log? (MySQL does, so you can see
the queries generated directly.)

You could also use print statements or a debugger to confirm that the
value of x[11].to_s.upcase is what you think it is.

I am out putting error messages to a file, but because I am running this
as a script in a simple editor and not as a true rails app, no log is
being produced except for what I output to an error file. I am out
putting the query and all the correct info should be found, so it must
be something minor that I am missing and most likely a simple bug.
Thanks for the reply,

-S

Shandy N. wrote in post #970912:

@finduser = User.find(:first, :conditions => [‘upper(username) = ?’,
x[11].to_s.upcase])

puts x[11].to_s.upcase.inspect

If x[11] is the last column in the CSV, you might find it has an
unexpected \n at the end. If so, you can chomp it off.

I guess this is not mysql, since mysql does case-insensitive comparisons
by default, so there’s no need for an upper() conversion

Beware: in other databases, you might find that the upper() conversion
defeats indexing of the column, so you could end up with a very
inefficient full table scan. Try an “explain plan” or equivalent to see.

On Mon, Dec 27, 2010 at 10:27 AM, Shandy N. [email protected]
wrote:

Does the DB you’re using have a log? (MySQL does, so you can see
the queries generated directly.)

I am out putting error messages to a file, but because I am running this
as a script in a simple editor and not as a true rails app, no log is
being produced except for what I output to an error file.

I didn’t ask about a application log – I asked about a DB query log,
which is totally independent of the app accessing it.

What DB are you using?