简体   繁体   English

如何在`find_by_sql`中传递字符串值

[英]how to pass string value in `find_by_sql`

Using query like:使用查询如:

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'")

Getting error after using sc.status='active' .Thanks.使用sc.status='active'后出错。 sc.status='active'

You can achieve that by using interpolation, here is an example from a project i made for doing something similar您可以通过使用插值来实现这一点,这是我为做类似事情而制作的项目中的一个示例

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")

As you can see #{Ticket::HIGH} is coming from PRIORITY = [HIGH = 'high', NORMAL = 'normal', LOW = 'low'] same goes for the others正如你所看到的#{Ticket::HIGH}来自PRIORITY = [HIGH = 'high', NORMAL = 'normal', LOW = 'low']其他人也是如此

Note: this is a part of the original code.注意:这是原始代码的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM