unscope
在rails中使用的重点是什么。
Article.where('id > 10').limit(20).order('id asc').unscope(:order)
# which gives
SELECT * FROM articles WHERE id > 10 LIMIT 20
# and this without unscope
SELECT * FROM articles WHERE id > 10 ORDER BY id asc LIMIT 20
但是为什么不删除该.order('id asc')
部分:
Article.where('id > 10').limit(20)
但是为什么不删除
.order('id asc')
我会说,原因之一是您可能根本无法访问原始查询,因此“修改”它的唯一方法是使用unscope
。
例如,
Article.where('id > 10').limit(20).order('id asc')
可能是某个范围内某些外部gem的一部分,因此您必须按原样使用它。这就是unscope
救援的时机。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句