mardi 29 avril 2014

Rails 4 [ActiveRecord]:Complex correlated query with Active record


Vote count:

0




I have the following (mysql) SQL query:



select a.id, a.name, count(*) as humans
from humans h1, areas a
where h1.gender = 'Female' and h1.area_id = a.id
group by area_id
having count(*) > (
select count(*)
from humans h2
where h2.gender = 'Male' and h1.area_id = h2.area_id
group by area_id
)
or not exists (
select *
from humans h2
where gender = 'Male' and h1.area_id = h2.area_id
group by area_id
)


Which basically shows all the areas which are dominated by female humans.


How exactly would I translate this to the active record syntax?


If it isn't supported by the standard active query, using squeel would also work, or is this just not possible?



asked 1 min ago

Aranir

132





Aucun commentaire:

Enregistrer un commentaire