如何使用arel调用postgres函数?
问题描述:
我称之为“move_to_end”我使用的find_by_sql如下调用Postgres的功能:如何使用arel调用postgres函数?
def move_to_end
self.class.find_by_sql ["select move_to_end(?)", id]
end
我想更换一个AREL调用的find_by_sql声明,但所有的例子,我发现要求arel与桌子一起工作。
有关如何完成此任何想法,将不胜感激。
答
您可以在Arel中使用NamedFunctions。 但是,您仍然必须使用arel_table来引用查询中的表列。一个可能的解决方法是用下划线别名它:
# == Schema Information
#
# Table name: solution
#
# solution_id :integer not null, primary key
# body :text
#
class Solution
class << self
alias_method :_, :arel_table
def only_checked
_.project(checked(_[:solution_id]))
end
def checked
Arel::Nodes::NamedFunction.new('CHECKED', [query])
end
end
end
Solution.only_checked.to_sql # -> SELECT CHECKED("solution"."solution_id") FROM "solution";
+0
这一工程......不得不重新安排postgres功能有点。谢谢! – TheDadman 2014-10-30 18:08:04
我觉得像'阿雷尔::节点:: SqlLiteral.new(“选择move_to_end ...”)' – mdesantis 2014-10-19 22:03:59