activerecord - count() and group() in a rails 3 query, or just use sql? -


what's rails 3 chained-method way kind of query?

@jobs_by_location =   employer.find_by_sql ['select count(j.id) job_count, e.* employers e, jobs j' +                         ' e.parent_id = ? , j.employer_id = e.id' +                         ' , j.status = 2' +                         ' group e.id' +                         ' order e.state_id, e.city, e.name asc', @employer.id] 

i came with:

@jobs_by_location = employer   .select('employers.*, count(jobs.id) job_count').joins(:jobs)   .group('employers.id').order('employers.state_id,employers.city,employers.name asc')   .where(:jobs => {:status => 2}).where(@employer.id) 

can tighten more? can clean order() call, , should using count() somewhere? should bother? thanks.

in order clause don't need specify table unless column name ambiguous. can do

.order('state_id, city, name asc') 

also, think meant put

 .where(:parent_id => @employer.id) # instead of .where(@employer.id) 

other think things fine. don't think .count case.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -