uno February 2016

Convert Inner Join to Left Join in rails

I have a rake file , and i have this code on it . .

   if circle[:conference2_total_channels].present?
                conf = Confbridge.find(:all, :conditions => ["confbridges.cid = ?",circle.circle_number],
                :joins => [:confbridge_participants],
                :select => 'confbridges.id, confbridges.confno, confbridges.max_member, (confbridges.max_member - 5) AS additional , (confbridges.max_member - IFNULL(confbridge_participants.active,0)) AS vacant')

            end

the return of the code is

SELECT confbridges.id, confbridges.confno, confbridges.max_member, (confbridges.max_member - 5) AS additional , (confbridges.max_member - IFNULL(confbridge_participants.active,0)) AS vacant FROM `confbridges` **INNER JOIN** `confbridge_participants` ON `confbridge_participants`.`confbridge_id` = `confbridges`.`id` WHERE (confbridges.cid = '0090000092')

it is INNER JOIN

how can i change it to LEFT JOIN

like this

SELECT confbridges.id, confbridges.confno, confbridges.max_member, (confbridges.max_member - 5) AS additional , (confbridges.max_member - IFNULL(confbridge_participants.active,0)) AS vacant FROM `confbridges` **LEFT JOIN** `confbridge_participants` ON `confbridge_participants`.`confbridge_id` = `confbridges`.`id` WHERE (confbridges.cid = '0090000092')

THANKS :)

Answers


J Plato February 2016

This is how I would write it:

Confbridge.where("confbridges.cid = ?",circle.circle_number)
  .joins("LEFT JOIN confbridge_participants 
         ON confbridge_participants.confbridge_id = confbridges.id")
  .select(...your select statement)

Post Status

Asked in February 2016
Viewed 3,240 times
Voted 4
Answered 1 times

Search




Leave an answer