本文介绍了如何在 ARel 中使用 CONCAT() 等函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法让 ARel 将(清理过的,可能别名等)列名写入 CONCAT() 和其他 SQL 函数中?
Is there a way to have ARel write (sanitized, possibly aliased, etc.) column names into CONCAT() and other SQL functions?
这里是如何使用 AVG()...
?> name = Arel::Attribute.new(Arel::Table.new(:countries), :name) => #<struct Arel::Attributes::Attribute [...] ?> population = Arel::Attribute.new(Arel::Table.new(:countries), :population) => #<struct Arel::Attributes::Attribute [...] ?> Country.select([name, population.average]).to_sql => "SELECT `countries`.`name`, AVG(`countries`.`population`) AS avg_id FROM `countries`"(是的,我知道 avg_id 在每一行都是一样的,只是想说明我的问题)
(yes, I know that avg_id would be the same in every row, just trying to illustrate my question)
如果我想要一个不同的功能怎么办?
So what if I wanted a different function?
?> Country.select(xyz).to_sql # Arel::Concat.new(name, population) or something? => "SELECT CONCAT(`countries`.`name`, ' ', `countries`.`population`) AS concat_id FROM `countries`"谢谢!
推荐答案Use NamedFunction:
name = Arel::Attribute.new(Arel::Table.new(:countries), :name) func = Arel::Nodes::NamedFunction.new 'zomg', [name] Country.select([name, func]).to_sql更多推荐
如何在 ARel 中使用 CONCAT() 等函数?
发布评论