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 7856191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:37:40+00:00 2026-06-02T20:37:40+00:00

We need to generate reports that pull in lots of data, run some calculations

  • 0

We need to generate reports that pull in lots of data, run some calculations and spit them out as part of a larger table. Doing this isn’t difficult. However, making it so that existing methods can be used and not generating 1000’s of SQL queries is a lot harder.

For instance I might have an Account class with a method like this:

def balance_at(time=Time.now)
  payments_out = self.payments.where("created_at <= ?",time).sum("amount")
  payments_in  = self.payments_on_account.where("created_at <= ?",time).sum("amount")
  payments_in - payments_out
end

This could be used to get an accounts balance say at the beginning of the month, and at the end. It works great.

However, if I want a table of all Account balances for the beginning and end of the month things get silly. So for example:

Account.includes(:payments, :payments_on_account)

Will get all of the data I’d need if I wanted to crunch this all purely in Ruby, but my nice little method balance_at doesn’t do all of the number crunching in Ruby (that would be slow for the individual case).

I could solve it with something that does it in Ruby and SQL depending on what is cached like so:

def balance_at(time=Time.now)
  payments_out, payments_in = [payments, payments_on_account].map{|payments|
    if payments.loaded?
      payments.find_all{|p| p.created_at < time }.inject(0){|a,p| p.amount + a }
    else
      payments.where("created_at <= ?",time).sum("amount")
    end
  }
  payments_in - payments_out
end

However, that isn’t terrible readable or easy to test either.

How would you solve it?

  • 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-02T20:37:41+00:00Added an answer on June 2, 2026 at 8:37 pm

    i’ve worked on a few projects that need reporting. the web application stack isn’t the best place to do your reporting, but it seems that the open source reporting options are fairly limited. but not every organization can use SSRS or Crystal and in my experience those products are painful and introduce more problems than necessary.

    I’m using views to get these done. SQL is designed for grouping and aggregating data and it’s more equipped to handle this stuff than ruby. however, most of the time, views are going to be executed on the fly, so it’s not like you’re getting a performance gain here. ideally, after you get a basic implementation, you can set up some cron tasks or means of precalculating the data. and if your reports are going to be accessed often and during the day, you will want a dedicated reporting db. if the reports have to have live data, you will need to set up replication.

    messing with SQL within Ruby/Rails is messy and frowned upon, I know. So I wrote a gem called Skiima and it helps you manage extraneous SQL objects that you may have in your project. And by loading them with your migrations, it becomes easier to test these.

    http://github.com/dcunited001/skiima

    aside from that, this is what i’ve been doing:

    class AccountsReport < ActiveModel
      attr_accessor :items
      def initialize(attr = {})
        # read in params, set attrs
      end
    
      def execute
        get_report_items
        group_report_items
        summarize_report_groups # if this needs to occur outside of sql
      end
    end
    
    class AccoutsReportItem < ActiveRecord::Base
      # you can hook into a view here, you will want the view to return an id col
      set_table_name :view_accounts_report_items
    end
    
    # yay for arel and activerecord methods.  
    # you can even set up relationships on these.  use sparingly.
    # AccountsReportItem.where(:blah => 'balah')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a lot of mysql data that I need to generate reports from.
I need to generate several reports, and many of them will simply be a
I need to generate some reports and the selected format has been FlowDocuments. As
I need to generate a file for Excel, some of the values in this
I've recently started using iTextSharp to generate PDF reports from data. It works quite
We've setup some different load tests and need to generate/send the result pr. e-mail,
I am creating a desktop app that will create some reports. I want to
I have a Crystal Reports project that I am working on where I need
I've got a project where I need to generate PDF reports from a template.
In Android applications, what is the best strategy to generate reports that can be

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.