2012-07-20 112 views

回答

0

嗯,我沒有意識到在rails中做這件事的好方法,但是我有一個原始的sql查詢,你可以使用ActiveRecord :: Base.connection.execute來觸發。讓我告訴你,這可能是我曾經建造的最醜陋的查詢,所以很抱歉:-)

SELECT ta.* FROM table_a AS ta INNER JOIN table_b AS tb ON ta.id = tb.table_a_id WHERE 
    tb.id IN (SELECT temp.id FROM (
    SELECT id, count(*) AS count FROM table_b GROUP BY table_a_id 
) AS temp WHERE temp.count=1) GROUP BY ta.id; 
相關問題