Introduction
I am making a system where I have a very simple layout consisting only of transactions (with basic CRUD). Each transaction has a date, type, debit amount (minus) and loan amount (plus). Think of an online banking application and more.
The problem I am facing is that my controller is accurate and worried about possibly over querying the database.
Simple report example
- Total debit for the selected period, for example.
SUM(debit) as total_debit - The total loan for the selected period, for example.
SUM(credit) as total_credit Total total, e.g. total_credit - total_debit
The report should contain a dynamic date range, for example. where(date BETWEEN 'x' and 'y')
- The date range will never be more than a year and will be maximum, for example, 1000 transactions / rows at a time
So, in the controller, I create:
def report @d = Transaction.select("SUM(debit) as total_debit").where("date BETWEEN 'x' AND 'y'") @c = Transaction.select("SUM(credit) as total_credit").where("date BETWEEN 'x' AND 'y'") @t = @c.credit_total - @d.debit_total end
Additional information about the question
My actual report is closer to 6 or 7 database queries (for example, pulling a total credit / debit according to type == 1 or type == 2, etc.) and has many other calculations, for example, to summarize certain types of credit / debit cards and then adding and removing these totals from other totals.
I try to stick with the "lean model, fat controller", but I am having problems with the number of variables that my controller must pass to the view. The rails seemed very simple until you create variables to jump to the view. I donβt understand how to do this, except that you add a variable that creates a string to the controller and makes it "skinnier", placing several request fragments in the model.
Is there something that I am missing when you create variables in the model and then pass the controller to these views?
source share