【发布时间】:2012-03-07 11:08:20
【问题描述】:
有没有办法让ARel 将列名写入(已清理,可能有别名等)CONCAT() 和其他 SQL 函数?
这里是how to do it with 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 在每一行都是一样的,只是想说明我的问题)
如果我想要一个不同的功能呢?
?> Country.select(xyz).to_sql # Arel::Concat.new(name, population) or something?
=> "SELECT CONCAT(`countries`.`name`, ' ', `countries`.`population`) AS concat_id FROM `countries`"
谢谢!
【问题讨论】:
-
Sequel 有一种将列选择为
"...".lit的方法,意思是“文字 SQL”,而不是在这种情况下被解释为字符串。这会禁用 SQL 转义,因此您可以注入任何您想要的东西。不确定 AREL 等价物是什么,但也许这是一个想法。 -
我自己在这里写了更多细节mrpunkin.com/post/18919379925/using-arel-for-sql-functions>
标签: ruby activerecord arel