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

  • SEARCH
  • Home
  • 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 4546396
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:56:55+00:00 2026-05-21T15:56:55+00:00

I’m working on generating quarterly royalty reports for authors. I have a Report model

  • 0

I’m working on generating quarterly royalty reports for authors. I have a Report model (not backed by a database table) that I’m using to display the report for specific authors based on the year and quarter. I’m routing that to author/:author_id/:reports/:year/:quarter

I’ve got some pretty ugly code in my controller right now, for the sake of getting things working. I can refactor later:

  def show
     @author = Author.find(params[:author_id])
     #find the orders that took place in the current quarter and year for the report
     @orders = Order.get_current_orders(quarter_range, params[:year])
     #find only the products that apply for the current author
     @author_products = @author.products
     #find only the line_items that match the current quarter's orders and the author's products
     @line_items = LineItem.find_all_by_order_id_and_product_id(@orders, @author_products)
     #group the line items by product
     @line_items_by_product = @line_items.group_by { |l| l.product_id }
   end

That let’s me do this in my view:

<%= @line_items_by_product.each do |product, line_item | %>
  <h3><%= product %></h3>
  <% line_item.each do |line_item| %>
    <%= line_item.quantity %> <br />
  <% end %>
<% end %>

Two things I need to fix. Right now, product just returns the product id, not the title (it’s stored in the products table, not the line_items table). I can’t access product.title here obviously, but I need to get the title in the grouping.

My second issue is that instead of just looping over the quantity of every single line item, I want to total the quantity of each line item and just display that. So in stead of getting , 1, 10, 55 … I just want 66. I tried array#inject, but I’m not grasping the syntax.

And, that all be said … I’m sure I’m doing more work than I need to. I started with a lot of the controller code in the model but was having a lot of undefined method errors. Any advice or help would be appreciated.

  • 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-05-21T15:56:56+00:00Added an answer on May 21, 2026 at 3:56 pm

    I agree that you need to refactor your code, and I’m only offering these answers to your specific questions – these are not my overall recommendations.

    First Problem

    Remember this is Ruby, a Product instance is just as valid to group by as the Integer that you’re currently using. So, you can change your group_by call to:

    @line_items_by_product = @line_items.group_by { |l| l.product }
    

    …and then change your view to…

    <h3><%= product.title %></h3>
    

    Second Problem

    <%= line_item.sum {|li| li.quantity } %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have a reasonable size flat file database of text documents mostly saved in
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a view passing on information from a database: def serve_article(request, id): served_article
I have two tables with like below codes: Table: Accounts id | username |
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.