postgresql - Database functions mapped to ActiveRecord Models -
let's suppose have 2 database functions (pl/pgsql):
create table balances (rents decimal(12,2) default 0 not null, expenses decimal(12,2) default 0 not null); create function account_balance(id integer) returns balances $$ select sum(case when t.amount > 0 t.amount else 0 end) rents, sum(case when t.amount < 0 t.amount else 0 end) expenses transactions t t.account_id = $1 $$ language 'sql'; create function tenant_balance(id integer) returns balances $$ select sum(case when t.amount > 0 t.amount else 0 end) rents, sum(case when t.amount < 0 t.amount else 0 end) expenses transactions t t.tenant_id = $1 $$ language 'sql';
how relate in activerecord models (in examples, account , tenant) can call account.first.balance
, returns valid instance of balance? speaking of balance, how should create model?
Comments
Post a Comment