我对一个Postgres数据库以下的find_by_sql查询:Rails - 如何防止postgres find_by_sql查询的sql注入?
pick_ids = picks.pluck(:id).join(',')
Pick.find_by_sql("WITH cte1 AS (SELECT DISTINCT user_id, state, pick FROM picks
WHERE id IN (#{pick_ids})), cte2 AS (SELECT user_id, coalesce(sum(amount_won), 0)
as picks_total_won FROM picks WHERE id IN (#{pick_ids}) GROUP BY user_id), cte3 AS
(SELECT user_id, COUNT(CASE WHEN state = 'won' then 1 ELSE null END) AS picks_won,
FROM cte1 GROUP BY user_id) SELECT cte2.picks_total_won, cte3.picks_won,
FROM cte2 INNER JOIN cte3 ON cte2.user_id = cte3.user_id")
当我尝试将其参数化(即,... pick_ids,pick_ids?),但是,我得到以下错误:
ArgumentError: wrong number of arguments (3 for 1..2)
(1)你能参数化一个find_by_sql查询吗?如果是这样,怎么样? (2)如果查询永远不会收到用户输入的参数,你甚至需要担心SQL注入吗?
太神奇了!很好的答案...应该对他人有用 – keruilin