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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:40:00+00:00 2026-05-21T11:40:00+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

But I’m using this:

@line_items_totals = LineItem.where(:order_id => @orders, :product_id => @author_products).order("product_id ASC").sum(:quantity, :group => :product_id)

That returns a hash with the product id as the key and the total quantity sold for all that products line items, for the given time period, like this:

#<Enumerator: {1=>2865, 2=>4068, 4=>50, 5=>60, 9=>22}>

the query it produces is:

SELECT SUM("line_items"."quantity") AS sum_quantity, product_id AS product_id 
FROM "line_items" 
WHERE ("line_items"."order_id" IN (10, 12, 15, 16)) 
AND ("line_items"."product_id" IN (2, 4, 1, 5, 9)) 
GROUP BY product_id 
ORDER BY product_id ASC

What I need, though, is the product title instead of the ID. Not having much luck. I tried using .joins — but it didn’t seem to work, because of the sum method in the query.

  • 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-21T11:40:00+00:00Added an answer on May 21, 2026 at 11:40 am

    I’d do:

    ListItem.other_stuff.joins(:product).group(:product_id).select('sum(line_items.quantity) as total_quantity, products.title')
    

    or

    LineItem.other_stuff.group(:product_id).select('sum(quantity) as total_quantity, (select title from products where products.id == product_id) as title)')
    

    then you can access title and total_quantity as LineItem properties:

    item.title
    item.total_quantity.to_i
    

    ActiveRecord or adapter dynamically define properties according to selected columns, even though there are no such columns in the table. Also notice, that for unknown reason MySQL adapter creates this dynamic attributes with String type, instead of Fixnum as you would expect (while SQLite adapter uses proper types). So you have to cast it manually, like i shown above.

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

Sidebar

Related Questions

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 have a reasonable size flat file database of text documents mostly saved in
I need a function that will clean a strings' special characters. I do NOT
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
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

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.