What is a Rails 3 method with a chain method to execute such a request?
@jobs_by_location = Employer.find_by_sql ['SELECT count(j.id) AS job_count, e.* FROM employers e, jobs j' + ' WHERE e.parent_id = ? AND j.employer_id = e.id' + ' AND j.status = 2' + ' GROUP BY e.id' + ' ORDER BY e.state_id, e.city, e.name ASC', @employer.id]
I figured it out:
@jobs_by_location = Employer .select('employers.*, count(jobs.id) as job_count').joins(:jobs) .group('employers.id').order('employers.state_id,employers.city,employers.name ASC') .where(:jobs => {:status => 2}).where(@employer.id)
Can I tighten this even more? Can I clear the order () call, and should I use count () somewhere? Should I worry? Thanks.
source share