Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write nested query in ruby on rails?

i want write a query for the following using where method

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

I tried using following query but ended with a syntax error

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

Please help

like image 748
nbbk Avatar asked Nov 03 '22 18:11

nbbk


1 Answers

hopefully this shud work!

Video.where("category = #{@static_cat} AND
      privacy = 'public' OR
      (privacy = 'private' AND
      id IN (?))",
      VidAd.where(:aid => current_id).select("vid"))
like image 141
rohith Avatar answered Nov 15 '22 04:11

rohith