Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8602757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:09:48+00:00 2026-06-12T02:09:48+00:00

I am building an Invoicing application where an Invoice has many Items and Payments.

  • 0

I am building an Invoicing application where an Invoice has many Items and Payments.

In my index view I am showing a list of all Invoices including two virtual attributes:

def total
  items.sum { |item| item.total }
end

def balance
  self.payments.sum(:amount) - self.total
end

I noticed that an aweful lot of SQL is needed to display the index view. Would it be advisable to create two more table columns instead? So far, I chose not to because I don’t like having too much redundant data.

This is my controller:

def index
  result = current_user.invoices.includes(:items, :payments)
  @invoices = paginate(result)
end

index.html.erb:

<table id="index">
  <thead>
    <tr>
      <th>Number</th>
      <th>Date</th>
      <th>Total</th>
      <th>Balance</th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <%= render @invoices %>
  </tbody>
</table>
<%= will_paginate @invoices %>

_invoice.html.erb:

<tr>
<td>
    <%= link_to invoice.number, invoice_path(invoice) %>
</td>
<td>
    <%= l invoice.date %>
</td>
<td>
    <%= number_to_currency(invoice.total) %>
</td>
<td>
    <%= number_to_currency(invoice.balance) %>
</td>       
<td>
    <%= destroy_link(invoice) %>
</td>
</tr>

For each invoice on the index view these four SQL queries are being generated:

(0.1ms)  SELECT SUM("payments"."amount") AS sum_id FROM "payments" WHERE "payments"."invoice_id" = 19
CACHE (0.0ms)  SELECT "items".* FROM "items" WHERE "items"."invoice_id" = 19
CACHE (0.0ms)  SELECT SUM("payments"."amount") AS sum_id FROM "payments" WHERE "payments"."invoice_id" = 19
CACHE (0.0ms)  SELECT "items".* FROM "items" WHERE "items"."invoice_id" = 19

(That makes 40 SQL queries per index page.)

I must admit that I am relatively new to Rails. So I wonder if there’s a Best Practice to follow?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:09:49+00:00Added an answer on June 12, 2026 at 2:09 am

    Probably the simplest improvement you could use here is eager loading.

    When you load your Invoice objects, if you eager load the associated items and payments, you’ll do 3 queries instead of, well, lots.

    So, if you have a controller action that does something like this:

    def index
      @invoices = Invoice.all
    end
    

    you could change it to:

    @invoices = Invoice.includes(:payments, :items).all
    

    This change should speed things up quite a bit, and doesn’t require that you change your total or balance methods – they still do the same thing, but fetch all the objects they need at once, instead of a few at a time.

    Now, this is still (potentially, at least) loading rather a lot of objects into memory. If you’re going to be displaying all sorts of data in that view related to the individual Items and Payments, there’s probably nothing to be done. But if all the view needs are the total and balance values, you can make your database do this for you and skip instantiating the objects, like this:

    @invoices = Invoice.select("invoices.*, sum(items.total) as item_total, (sum(payments.amount) - sum(items.total)) as remaining_balance").joins(:items, :payments).group('invoices.id')
    

    When you use a custom select clause like this, the extra column get grafted onto the returned objects as attributes with the name of the column, so you can do:

    @invoices.first.remaining_balance
    

    I’ve used different names for these columns so that they don’t overlap with your existing balance and total methods – you may or may not still need those, as these values only exist when the objects they’re called on use the custom select described. If they were loaded without it, trying to call .remaining_balance would produce a NoMethodError.

    Caveats: I’m using PostgreSQL 9.1.3, if you’re not, the above may need to be modified slightly. Also, the adapter has an odd tendency to return these values as strings, so you may need to call .to_f or something similar on them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a script which has to patch XML files, including replacing one list
I´m building a small application with Jquery and PHP. Jquery (index.html) adds Form Fields
I am building a small application for a friend, basically it is for invoicing
For my company, we are building a web application for an invoicing system which
I'm building very basic mvc application with Spring. It has one controller that should
I'm building an application which basically has a lot of user input, text boxes,
Building an inventory system. I have lots of products and each product has three
Building a website that has English & Japanese speaking users, with the Japanese users
Building my first SL MVVM application (Silverlight4 RC) and have some issues i don't
Building a new ASP.net application, and planning to separate DB, 'service' tier and Web/UI

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.