2012-10-15 3 views
1

에 쿼리를 작성하는 방법 ruby에서 아래 쿼리를 작성하려면 어떻게합니까?ruby에서 SQL 쿼리

select count(*), project_types.project_type_name,company_id from project_type_stages_questions,project_types where (project_type_stage_id in 
(select project_type_stages_id from project_type_stages where project_type_id in (select project_type_id from project_types))) group by project_type_stage_id,project_types.project_type_name,company_id 
+0

유사한 문제가 설명 된 다음 게시물을 읽어보십시오. http://stackoverflow.com/questions/2894807/nested-queries-in-arel –

답변

0

당신이 할 수있는 복잡한 SQL을 가지고, 당신은 그 결과를 사용하고자하는 경우에는 DB 관리자에게 전달할 수 있습니다

sql = "select count(*), project_types.project_type_name,company_id from project_type_stages_questions,project_types where (project_type_stage_id in (select project_type_stages_id from project_type_stages where project_type_id in (select project_type_id from project_types))) group by project_type_stage_id,project_types.project_type_name,company_id" 
ActiveRecord::Base.connection.execute(sql) 

공개 API를 많이 제공하지 않습니다 정보이지만, here입니다.

결과로 ActiveRecord :: Relation이 필요한 경우 Arel을 사용하는 것이 좋습니다.