2013-07-17 74 views
5

我想寫出下面使用的查詢,其中方法如何在Ruby on Rails中編寫嵌套查詢?

SELECT * FROM videos 
WHERE 'privacy' = 'public' OR 
     (privacy = 'private' AND 
     id IN (SELECT vid 
       FROM vid_ads 
       WHERE 'aid'=#{current_id})) 

我嘗試使用下面的查詢但有語法錯誤結束

Video.where("privacy = 'public' OR 
      (privacy = 'private' AND id = ?)", 
      VidAd.where(:aid => current_id).select("vid")) 

請幫

回答

2

希望這shud工作!

Video.where("category = #{@static_cat} AND 
     privacy = 'public' OR 
     (privacy = 'private' AND 
     id IN (?))", 
     VidAd.where(:aid => current_id).select("vid"))