Uday kumar das
Uday kumar das

Reputation: 1613

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.

Upvotes: 1

Views: 327

Answers (1)

Mostafa Hussein
Mostafa Hussein

Reputation: 11940

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

Note: this is a part of the original code.

Upvotes: 1

Related Questions