2014-04-08 22 views
0

如何将以下sql查询转换为Ruby on Rails?如何将SQL查询转换为Ruby on Rails

select health_workers.name,health_workers.surname,clinics.name as clinic,count(observations.id) as count,observations.observation_date 
from health_workers 
left join observations on health_workers.id = observations.health_worker_id, clinics 
where health_workers.clinic_id = clinics.id 
group by health_workers.name,health_workers.surname,clinics.name,observations.observation_date. 

回答

0

你可以做这样

query = "select health_workers.name,health_workers.surname,clinics.name as clinic,count(observations.id) as count,observations.observation_date 
from health_workers 
left join observations on health_workers.id = observations.health_worker_id, clinics 
where health_workers.clinic_id = clinics.id 
group by health_workers.name,health_workers.surname,clinics.name,observations.observation_date." 


results = ActiveRecord::Base.connection.execute(query) 

,这将让你的结果