I have created a Postgres function that I am using in order to perform a complex query which joins many tables that all have to be filtered by a dynamic date field.
The function works perfectly, and allows my to perform a query like "SELECT * FROM trail_for_date('2014-01-01')" and returns a table.
A simplified example from the Postgres documentation on functions:
CREATE FUNCTION sum_n_product_with_tab (x int)
RETURNS TABLE(sum int, product int) AS $$
SELECT $1 + tab.y, $1 * tab.y FROM tab;
$$ LANGUAGE SQL;
How could I use this return table as a Rails/Ruby model where the argument to the function is dynamic?
Something like the following (which obviously doesn't work):
class SimplifiedExample < ActiveRecord::Base
self.table_name = 'sum_n_product_with_tab(:dynamic_input)'
end
Model.select(Arel::Nodes::NamedFunction.new('sum_n_product_with_tab', 6)).to_sql