如何将SQL查询转换为Ruby on Rails

时间:2014-04-08 16:00:56

标签: sql ruby-on-rails

如何在rails上将以下sql查询转换为ruby?

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.

1 个答案:

答案 0 :(得分: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)

这将为您提供结果