4
什麼是Rails 3鏈式方法來做這種查詢?在rails 3查詢count()和group(),或者只是使用sql?
@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]
我想出了:
@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)
我可以收緊這件事更?我可以清理order()調用,並且我應該在某處使用count()嗎?我應該打擾嗎?謝謝。