繁体   English   中英

如何按 id 数组对 ActiveRecord 对象进行排序

[英]How can I sort ActiveRecord Objects by id array

我的环境

ruby 2.0.0-p195
rails (4.0.0.rc1)
activerecord (4.0.0.rc1)

我想按 id 数组对 ActiveRecord 对象进行排序。 我尝试按字段排序。

ids = [1,4,2,3]
Foo.where(id: ids).order('FIELD(id, ?)', ids)

然而它失败了。

Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?,?,?,?}), ö, ÷, ï, ñ' at line 1:

然后我试试

ids = [1,4,2,3]
Foo.where(id: ids).order('FIELD(id, #{ids.join(","))')

这当然是成功。 但是我担心它可能有 SQL 注入风险,因为数组 ID 是从会话值生成的。

有没有更好更安全的方法?

提前致谢。

你是对的,这是一个安全漏洞。

我看到两种可能性:

转换为数字

ids = [1,4,2,3].map(&:to_i).compact
Foo.where(id: ids).order("FIELD(id, #{ids.join(',')})")

我认为这应该是安全的,因为您确保值是数字,然后删除 nil 条目。 nil 条目将是非数字条目。

转义字符串

escaped_ids = ActiveRecord::Base::sanitize(ids.join(","))
Foo.where(id: ids).order("FIELD(id, #{escaped_ids})")

你只是转义你的字符串的内容......没什么特别的。

我在 Rails6 上使用 FIELD 方法尝试了这个答案,但遇到了错误。 但是,我发现所要做的就是将 sql 包装在Arel.sql()

# Make sure it's a known-safe values.
user_ids = [3, 2, 1]
# Before 
users = User.where(id: user_ids).order("FIELD(id, 2, 3, 1)")
# With warning.

# After 
users = User.where(id: user_ids).order(Arel.sql("FIELD(id, 2, 3, 1)"))
# No warning

[1] https://medium.com/@mitsun.chieh/activerecord-relation-with-raw-sql-argument-returns-a-warning-exception-rising-8999f1b9898a

暂无
暂无

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

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