Rich Collins successfully answered Dave's question:

Hey Rich,

What happened to your previous question...it *seems* like it got answered by besquared, but the answer does not show up at all. It looks like maybe only the tail end of the chat log was saved.

At any rate (even though you may not need the answer any more), here's how I would have done it:

First, in the find function, you use userid, but there is no userid field in user. Thus, the two places where you have userid should be prefixed with posts., as in posts.userid

If that doesn't work, I'd go straight to SQL using the find_by_sql function:

Users.find_by_sql "SELECT u.*, count(p.userid) postcount FROM users u, posts p WHERE p.userid = u.id GROUP BY p.userid ORDER BY postcount"

People succeed in answering Dave's questions 50% of the time (2 successes in 4 attempts).

Answers by: Rich Collins

Rich Collins's Answer:

Reply by Rich Collins 777 days ago

Ah he just posted our conversation on IRC ;)

The solution is to use the counter_cache feature of rails (which I had forgotten about). It keeps track of the number of associated objects.

So if I have users and posts, it will keep track of the number of posts in a field in the users table called posts_count.