我一直对以下查询感到困惑,以及如何使用活动记录来完成。
select * from links where id in
(select id from
(select votable_id as id, count(votable_id) as count from votes where vote_scope = 'flag' and votable_type = 'Link' group by votable_id )
as x where count < 3);
我目前在我的 finder 中使用原始 SQL 查询find_by_sql
(
编辑
感谢 Joachim Isaksson,查询可以压缩到
select * from links where id in
(select votable_id from votes
where vote_scope = 'flag'
and votable_type = 'Link'
group by votable_id
HAVING COUNT(*) < 3) ;