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