Rails ActiveRecord group_by & sum db results for use with Lazy HighCharts

后端 未结 1 1119
眼角桃花
眼角桃花 2021-01-06 09:56

I am completely new to RoR/Ruby and i am using Lazy High Charts gem to generate some purdy charts based on some database information.

I have tried the answers that we

相关标签:
1条回答
  • 2021-01-06 10:29

    It looks like you have everything in place. Here's how you can pull data from db:

    @aggregated_invoices = Invoice.
      where(:account_id => params[:id]).
      order("invoice_date DESC").
      group("invoice_date").
      select("DATE_FORMAT(invoice_date, '%Y-%m-01') AS invoice_date, sum(amount_used) AS amount_used, sum(billed_amount) AS billed_amount")
    
    # Then use these instead of sample data:
    @categories = @aggregated_invoices.map {|i| i.invoice_date.strftime("%b/%Y")}
    @amount_used_data = @aggregated_invoices.map(&:amount_used)
    @billed_amount_data = @aggregated_invoices.map(&:billed_amount)
    
    0 讨论(0)
提交回复
热议问题