如何在rails中使用左连接?

时间:2016-02-08 06:05:18

标签: mysql ruby-on-rails ruby

我正在使用rake文件,这是代码

task :terminate_conference_channel, [ :circle ] => :environment do |t, args|
    abort "no circle" unless args[:circle]
    ActiveRecord::Base.transaction do
      begin
        circlenum = args[:circle]
        circle = Circle.find_by circle_number: circlenum
        if circle.present?
            if circle[:conference2_total_channels].present?
                conf = Confbridge.find(:first, :conditions => ["confbridges.cid = ?",circle.circle_number],

            end

        end
      rescue Exception => e
        ActiveRecord::Rollback
        logger.info "Error Terminating"
      end
    end
end

如何使用join

获取select语句

我怎样才能将这个版本转换成轨道?

SELECT
    c.id,
    c.confno,
    c.max_member,
    (c.max_member - 5) AS additional,
    (c.max_member - IFNULL(p.active,0)) AS vacant
FROM
    confbridges c
LEFT JOIN
    (SELECT
        confbridge_id,
        COUNT(uid) AS active
    FROM
        confbridge_participants
    WHERE
        active = 1
    GROUP BY
        confbridge_id) AS p
    ON c.id = p.confbridge_id
WHERE
    cid = '0090000092'
ORDER BY
    vacant DESC
conf = Confbridge.find(:first, :conditions => ["confbridges.cid = ?",circle.circle_number],

这是我的模特

class Confbridge < ActiveRecord::Base
  has_many :confbridge_schedules, :dependent => :destroy

  validates :confno, uniqueness: true, :allow_nil => true, :allow_blank => true

end

谢谢:)

0 个答案:

没有答案