如何使用功能,如CONCAT()等,阿雷尔? [英] How do I use functions like CONCAT(), etc. in ARel?
本文介绍了如何使用功能,如CONCAT()等,阿雷尔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法让阿雷尔
写(消毒,可能别名等)列名到 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`"
谢谢!
推荐答案
使用 NamedFunction
:
name = Arel::Attribute.new(Arel::Table.new(:countries), :name)
func = Arel::Nodes::NamedFunction.new 'zomg', [name]
Country.select([name, func]).to_sql
这篇关于如何使用功能,如CONCAT()等,阿雷尔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文