If you use a counter cache then this happens for free, if not something like Farmer.find_by_sql <<_SQL select farmers.*, count(*) as chicken_count from farmers left outer join chichens on farmer_id = farmers.id group by farmers.id order by chicken_count desc SQL
would probably do the job
Fred