1

How do I write the below query in ruby.

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
Jimmy
  • 16,123
  • 39
  • 133
  • 213
Vish
  • 89
  • 1
  • 3
  • 13
  • Read the following post, when a similar issue is explained: http://stackoverflow.com/questions/2894807/nested-queries-in-arel – Itay Grudev Oct 15 '12 at 09:03

1 Answers1

0

If you have a complex sql to do, and you're going to use its result, you can pass it to the db manager:

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)

The public api don't provide much information, but it's here.

If you need an ActiveRecord::Relation as result, them I suggest to use Arel.

Alejandro Babio
  • 5,189
  • 17
  • 28