使用 Rails AREL 的复杂订单语句:SQL Case 语句
我有这段代码,基本上尝试在活动关系顺序方法中使用 SQL case 语句:
relation = Foo.order("CASE WHEN foos.thing IS NOT NULL THEN 0 ELSE 1 END ASC")
在生成(和执行)的 SQL 中,它显示为:
(ORDER BY CASE ASC)
我尝试深入挖掘源代码并丢失在 Visitor.access 调用中线程向下。这是一个已知问题吗?是用户错误吗?我必须做一些神奇的事情才能让它发生吗?我的印象是它只是插入了原始 SQL。我们还对关系执行其他操作,例如选择、限制、偏移、分组、拥有和连接。
帮助! :)
I've got this bit of code that basically tries to use a SQL case statement in the active relation order method:
relation = Foo.order("CASE WHEN foos.thing IS NOT NULL THEN 0 ELSE 1 END ASC")
and in the generated (and executed) SQL it comes up as:
(ORDER BY CASE ASC)
I've tried digging down into the source and lose the thread down in the visitor.access call. Is this a known issue? Is it user error? Is there some magical thing I have to do to make it happen? I was under the impression that it just inserted the raw SQL. There are other things we're doing with the relation, such as select, limit, offset, group, having and joins.
help! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也遇到过这个问题:
您可以将 CASE 放入 SELECT 中并命名它,以便您可以在 ORDER BY 中使用它。
I've had this problem too:
You can put the CASE into the SELECT and name it so you can use it in the ORDER BY.