私は、次のクエリと、アクティブ レコードを使用してそれを実行する方法について、頭を悩ませてきました。
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);
現在、ファインダーで生の 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) ;