AR/Arel - 我如何编写查询来选择列的条件 CONCAT
我有一个模型方法,如果用户保存了真实姓名,则有条件地连接用户的用户名(“登录”)和真实姓名 - 否则它只显示用户名。我想在 ActiveRecord 或 Arel 中重写查询。
看起来我应该使用 Arel::Nodes::NamedFunction
。但我不明白如何使用命名函数进行条件连接。 (Arel 知道“if”吗?我在文档中找不到任何参考。)
def primer_values
connection.select_values(%(
SELECT CONCAT(users.login,
IF(users.name = "", "", CONCAT(" <", users.name, ">")))
FROM users
ORDER BY IF(last_login > CURRENT_TIMESTAMP - INTERVAL 1 MONTH,
last_login, NULL) DESC,
contribution DESC
LIMIT 1000
)).uniq.sort
end
ORDER BY 中也有类似的条件。
I've got a model method that conditionally concatenates the user's username ("login") and real name, if they've saved a real name - otherwise it just shows the username. I'd like to rewrite the query in ActiveRecord or Arel.
It looks like I should use an Arel::Nodes::NamedFunction
. But i don't understand how to do the conditional concatenation with a named function. (Does Arel know about "if"? I can't find any reference in the docs.)
def primer_values
connection.select_values(%(
SELECT CONCAT(users.login,
IF(users.name = "", "", CONCAT(" <", users.name, ">")))
FROM users
ORDER BY IF(last_login > CURRENT_TIMESTAMP - INTERVAL 1 MONTH,
last_login, NULL) DESC,
contribution DESC
LIMIT 1000
)).uniq.sort
end
There's also similarly a conditional in ORDER BY.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然我通常讨厌 Rails 中的原始 SQL,但考虑到这种用法,我会保留它原样。尽管我可能会将其更改为更惯用的内容。
将其转换为 Arel 而不将其抽象为自己的对象会严重损害可读性。
这么说只是为了给你一个想法;第一部分是 3 个
NamedFunction
A
NamedFunction
是FUNCTION_NAME(ARG1,ARG2,ARG3)
的构造函数,因此任何 SQL使用此语法可以使用NamedFunction
创建,包括空函数,如NOW()
或其他语法,如横向(查询)
。While generally I abhor Raw SQL in rails given this usage I'd leave it as is. Although I might change it to something a bit more idiomatic like.
Converting this to Arel without abstracting it into an object of its own will damage the readability significantly.
That being said just to give you an idea; the first part would be 3
NamedFunction
sA
NamedFunction
is a constructor forFUNCTION_NAME(ARG1,ARG2,ARG3)
so any SQL that uses this syntax can be created usingNamedFunction
including empty functions likeNOW()
or other syntaxes likeLATERAL(query)
.