How to call postgres function using isl?

I have a Postgres function called move_to_end that I call using find_by_sql as shown below:

def move_to_end
  self.class.find_by_sql ["select move_to_end(?)", id]
end

      

I'd like to replace the find_by_sql statement with an isl call, but all the examples I find require the isl command to work with a table.

Any thoughts on how to achieve this would be appreciated.

+3


source to share


1 answer


You can do this using NamedFunctions in Arela. However, you still have to use isl_table to refer to the table column in the query. One possible workaround is to underline it:



# == 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";

      

+2


source







All Articles