我试图通过在连接上用我的参数替换来避免任何 SQL 注入漏洞。
Category.joins("LEFT OUTER JOIN incomes ON incomes.category_id = categories.id AND incomes.dept_id = ?", params[:Dept])
这会尝试执行带有问号的查询,而不是用它代替参数。这样做的正确方法是什么?
编辑:
查询需要返回:
SELECT categories.*
FROM "categories"
LEFT OUTER JOIN incomes
ON incomes.category_id = categories.id AND incomes.dept_id = 86
不是
SELECT categories.*
FROM "categories"
LEFT OUTER JOIN incomes
ON incomes.category_id = categories.id
WHERE incomes.dept_id = 86
非常不同的结果!