[英]how to pass string value in `find_by_sql`
使用查詢如:
Campaign.find_by_sql("select c.*,sc.*,org.name as org_name from campaigns as c left join super_campaigns as sc ON sc.id= c.super_campaign_id left join organisations as org ON org.id= c.organisation_id where c.status=0 AND sc.status='active'")
使用sc.status='active'
后出錯。 sc.status='active'
。
您可以通過使用插值來實現這一點,這是我為做類似事情而制作的項目中的一個示例
self.find_by_sql("SELECT s.subject_title AS subject_name,s.subject_code AS subject_code,
COUNT(*) AS total_complaints,
COUNT(CASE p.priority_name WHEN '#{Ticket::HIGH}' THEN 1 END) AS high_complaints,
COUNT(CASE p.priority_name WHEN '#{Ticket::NORMAL}' THEN 1 END) AS normal_complaints,
COUNT(CASE p.priority_name WHEN '#{Ticket::LOW}' THEN 1 END) AS low_complaints
FROM
tickets AS t
JOIN
subjects AS s
ON t.subject_id = s.id
JOIN
priorities AS p
ON t.priority_id = p.id
WHERE
p.priority_name IN ('#{Ticket::HIGH}', '#{Ticket::NORMAL}', '#{Ticket::LOW}')
GROUP BY s.subject_title, s.subject_code
ORDER BY total_complaints ASC")
正如你所看到的#{Ticket::HIGH}
來自PRIORITY = [HIGH = 'high', NORMAL = 'normal', LOW = 'low']
其他人也是如此
注意:這是原始代碼的一部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.