Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the 'Rails Way' to implement a dynamic reporting system on data
    primarykey
    data
    text
    <h3>Intro</h3> <p>I'm doing a system where I have a very simple layout only consisting of transactions (with basic CRUD). Each transaction has a date, a type, a debit amount (minus) and a credit amount (plus). Think of an online banking statement and that's pretty much it.</p> <p>The issue I'm having is keeping my controller skinny and worrying about possibly over-querying the database.</p> <h3>A Simple Report Example</h3> <ul> <li>The total debit over the chosen period e.g. <code>SUM(debit) as total_debit</code></li> <li>The total credit over the chosen period e.g. <code>SUM(credit) as total_credit</code></li> <li><p>The overall total e.g. <code>total_credit - total_debit</code></p></li> <li><p>The report must allow a dynamic date range e.g. <code>where(date BETWEEN 'x' and 'y')</code></p></li> <li>The date range would never be more than a year and will only be a max of say 1000 transactions/rows at a time</li> </ul> <p>So in the controller I create:</p> <pre><code>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 </code></pre> <h3>Additional Question Info</h3> <p>My actual report has closer to 6 or 7 database queries (e.g. pulling out the total credit/debit as per type == 1 or type == 2 etc) and has many more calculations e.g totalling up certain credit/debit types and then adding and removing these totals off other totals.</p> <p>I'm trying my best to adhere to 'skinny model, fat controller' but am having issues with the amount of variables my controller needs to pass to the view. Rails has seemed very straightforward up until the point where you create variables to pass to the view. I don't see how else you do it apart from putting the variable creating line into the controller and making it 'skinnier' by putting some query bits and pieces into the model.</p> <p>Is there something I'm missing where you create variables in the model and then have the controller pass those to the view?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload