find method not working

1 view
Skip to first unread message

TomB

unread,
Jun 29, 2007, 5:43:25 PM6/29/07
to Ruby on Rails: Talk, t...@bajzek.com
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.)

Adam Parrott

unread,
Jun 30, 2007, 7:26:44 AM6/30/07
to rubyonra...@googlegroups.com
What are the results of this?

@expense = Expense.find(:all, :conditions => "clientid = 'tbg'")

- Adam

TomB

unread,
Jul 12, 2007, 3:09:56 PM7/12/07
to Ruby on Rails: Talk
Adam,

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

Tom

On Jun 30, 7:26 am, "Adam Parrott" <pingusk...@gmail.com> wrote:
> What are the results of this?
>
> @expense = Expense.find(:all, :conditions => "clientid = 'tbg'")
>
> - Adam
>

Mike Garey

unread,
Jul 12, 2007, 3:21:17 PM7/12/07
to rubyonra...@googlegroups.com
have you checked the sql that it's generating and tried to enter that
manually into your sql client?

Mike

Matthew Rudy

unread,
Jul 12, 2007, 3:27:32 PM7/12/07
to rubyonra...@googlegroups.com
yeah,
I suggest you tail log/development.log
and see what query is going in there.

How about printing out the results of this from the console;
"Expense.find_all_by_clientid('tbg').map(&:clientid)"

clearly the query at least thinks its right.

Mike Garey wrote:
> have you checked the sql that it's generating and tried to enter that
> manually into your sql client?
>
> Mike


--
Posted via http://www.ruby-forum.com/.

TomB

unread,
Jul 12, 2007, 3:32:52 PM7/12/07
to Ruby on Rails: Talk
Mike,

On Jul 12, 3:21 pm, "Mike Garey" <random...@gmail.com> wrote:
> have you checked the sql that it's generating and tried to enter that
> manually into your sql client?
>
> Mike
>

TomB

unread,
Jul 12, 2007, 3:39:03 PM7/12/07
to Ruby on Rails: Talk
Mike,

It does 3 queries:

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

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

On Jul 12, 3:21 pm, "Mike Garey" <random...@gmail.com> wrote:

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

TomB

unread,
Jul 12, 2007, 3:46:35 PM7/12/07
to Ruby on Rails: Talk
Matthew,

On Jul 12, 3:27 pm, Matthew Rudy <rails-mailing-l...@andreas-s.net>
wrote:

TomB

unread,
Jul 12, 2007, 3:50:17 PM7/12/07
to Ruby on Rails: Talk
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

On Jul 12, 3:27 pm, Matthew Rudy <rails-mailing-l...@andreas-s.net>
wrote:

Mike Garey

unread,
Jul 12, 2007, 4:04:07 PM7/12/07
to rubyonra...@googlegroups.com
On 7/12/07, TomB <tba...@gmail.com> wrote:
>
> Mike,
>
> It does 3 queries:
>
> SELECT * FROM expenses WHERE (clientid = 'tbg')
> SELECT COUNT(*) FROM expenses
> SELECT * FROM expenses LIMIT 0, 10
>
> 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)

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

TomB

unread,
Jul 12, 2007, 4:38:54 PM7/12/07
to Ruby on Rails: Talk
Mike,

Yes, the most recent code being:

@expense = Expense.find(:all, :conditions => "clientid = 'tbg'")

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

Tom

On Jul 12, 4:04 pm, "Mike Garey" <random...@gmail.com> wrote:

Mike Garey

unread,
Jul 12, 2007, 5:15:34 PM7/12/07
to rubyonra...@googlegroups.com
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


On 7/12/07, TomB <tba...@gmail.com> wrote:
>

TomB

unread,
Jul 12, 2007, 5:56:12 PM7/12/07
to Ruby on Rails: Talk
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

On Jul 12, 5:15 pm, "Mike Garey" <random...@gmail.com> wrote:
> 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
>

Reply all
Reply to author
Forward
0 new messages