find method not working

I'm learning Rails. I have a simple find that doesn't work:

@expense = Expense.find_all_by_clientid('tbg')

There a number of records in the MySQL DB with clientid = 'tbg' but the find returns all records, including those with other values for clientid. I've tried at least a half-dozen versions of find, including find (by itself),and find_by_sql with appropriate parameters as given in Rails docs online and Agile Web Development with Rails (p212++). No matter how I try this retrieval on the Web, it always returns all records in the DB. When I did the same find in the terminal, it worked, only returning the records where clientid = 'tbg'

Is t here a reason for this? What am I missing?

(I'm running on MacOSX-10.4.9, on a PB-G4, 1.5GHz, using Standard Rails Mar 2007 running under Locomotive 2.0.8.)

What are the results of this?

@expense = Expense.find(:all, :conditions => “clientid = ‘tbg’”)

  • Adam

Adam,

The results are exactly the same as in the case I submitted- all records are retrieved.

Tom

have you checked the sql that it's generating and tried to enter that manually into your sql client?

Mike

Mike,

Mike,

It does 3 queries:

  SELECT * FROM expenses WHERE (clientid = 'tbg')   SELECT COUNT(*) FROM expenses   SELECT * FROM expenses LIMIT 0, 10e

The first one works correctly in CocoaSQL, as I'd expect The second one is presumably to find out whether it needs to pageinate the results The third on retrieves all records, and all records are displayed (there happen to be 9 of them)

I notice there is no WHERE clause in this query, which looks like a problem to me

It later does another query: SHOW FIELDS FROM expenses, which works.

Tom

Matthew,

Matthey,

  The result is from the console is:

Expense.find_all_by_clientid('tbg').map(&:clientid)

=> ["tbg", "tbg", "tbg", "tbg", "tbg"]

  This suggests to me that it has found 5 records, which is the correct number that match.

Tom

Why is there a limit clause? I see no limit parameter to your original find. Are you paginating the results after your find? It looks like you're doing the following:

@expense = Expense.find_all_by_clientid('tbg')

@expenses_pages, @expenses = paginate :expenses, :per_page => 10

Are you?

Mike

Mike,

  Yes, the most recent code being:

    @expense = Expense.find(:all, :conditions => "clientid = 'tbg'")     @expense_pages, @expenses = paginate :expenses, :per_page => 10

Tom

you've got two different arrays there. The @expense array contains the list of filtered expenses, containing only those expense records whose clientid is tbg. The second array, @expenses, contains _all_ expenses, with no conditions applied, other than a limit of 10 per page. You need to add your conditions to your paginate method call.

You should take a look at the following post for more information:

http://tinyurl.com/35m7e7

you can find the will_paginate plugin here: http://errtheblog.com/post/4791

Mike

Mike,

  This worked, which seems perfectly reasonable now that I've seen it. I'd just not recognized the need to do it this way before your pointed it out.

Thanks much, Tom