在某些控制器操作上出现以下警告。
DEPRECATION WARNING: Dangerous query method (method whose arguments are used as raw SQL) called with non-attribute argument(s):
"CASE id WHEN 343[...]". Non-attribute arguments will be disallowed in Rails 6.0.
This method should not be called with user-provided values,
such as request parameters or model attributes. 但是,“用户提供的”值并不调用此方法:
def find_ordered(ids)
order_clause = "CASE id "
ids.each_with_index do |id, index|
order_clause << "WHEN #{id} THEN #{index} "
end
order_clause << "ELSE #{ids.length} END"
where(id: ids).order(order_clause)
end它确实调用了模型属性。那么,如何对这个初始化程序方法进行同步以使Rails 6可以接受呢?
发布于 2020-07-23 16:45:42
它不是严格的“用户提供的值”,但是Rails无法知道字符串是来自用户还是在程序中是硬编码的。
解决这个问题的方法是使用Arel.sql
封装了一个已知安全的SQL字符串,用于传递查询方法。
where(id: ids).order(Arel.sql(order_clause))https://stackoverflow.com/questions/63058826
复制相似问题