具有以下sql查询,如何转换Active Record中两列的乘法?
SELECT plan_name, plan_price, count(plan_id), plan_price*count(plan_id) AS totalrevenue
FROM leads
INNER JOIN plans p ON leads.plan_id = p.id
WHERE lead_status_id = 5
GROUP BY plan_name, plan_price;
答案 0 :(得分:0)
我会尝试这样的事情。
leads = Lead.joins(:plans).
where(lead_status_id: 5).
group(:plan_name, :plan_price).
select('plan_name, plan_price, count(plan_id), plan_price * count(plan_id) AS totalrevenue')
leads.each { |lead| puts lead.totalrevenue }
注意:
joins(:plan)
还是joins(:plans)
,这取决于您的关联plan_id
,我都会在该字段中甚至给一个名字totalrevenue
之类的字段,因为它不是对象的一部分,而是虚拟属性