ruby on rails - Use Postgres Function as ActiveRecord Model -


i have created postgres function using in order perform complex query joins many tables have filtered dynamic date field.

the function works perfectly, , allows perform query "select * trail_for_date('2014-01-01')" , returns table.

a simplified example postgres documentation on functions:

create function sum_n_product_with_tab (x int) returns table(sum int, product int) $$     select $1 + tab.y, $1 * tab.y tab; $$ language sql; 

how use return table rails/ruby model argument function dynamic?

something following (which doesn't work):

class simplifiedexample < activerecord::base   self.table_name = 'sum_n_product_with_tab(:dynamic_input)' end 

create view containing data need , can create activerecord model access it.

you haven't provided specific details of data simple example, create view in postgres collect data;

  create or replace view data_trails     select t.*, td.trail_date trails t     join trail_dates td on (td.trail_id = t.id) 

then create model

  class datatrail < activerecord::base     scope :on_date, -> (date) { where(trail_date: date) }   end    datatrail.on_date(date.today) 

you can find more information in enterprise rails book. it's getting little dated principles sound.

http://enterpriserails.chak.org/full-text/chapter-11-view-backed-models


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -