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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:29:30+00:00 2026-05-29T21:29:30+00:00

For a rails 3 class like class Item < ActiveRecord::Base has_many :production_runs has_many :deliveries

  • 0

For a rails 3 class like

class Item < ActiveRecord::Base
  has_many :production_runs
  has_many :deliveries
end

where both production runs and deliveries have the attribute “quantity” for the number of items in that particular production or delivery.

I would like to query items and (perhaps using a scope?) add the Sum() of the quantities from both tables to the result. Something like:

select items.*, (select SUM(production_runs.quantity) as runs from production_runs where production_runs.item_id = 42), (select SUM(deliveries.quantity) as dels from deliveries where deliveries.item_id = 42) from items where items.id = 42;

Which gives the correct result for a single item, but I’d like to somehow roll those sums into a more general query.

I have tried a variety of adding scopes to both Item and the ProductionRun and Delivery classes with no luck. It would be nice to use a scope as I could chain it with other conditions as needed, but really i’d be happy with any general solution that was more optimal than just finding sets of items and then iterating.

Anyone know the best way to do this?

Thanks

  • 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-29T21:29:31+00:00Added an answer on May 29, 2026 at 9:29 pm
    scope :sum_column_name, lambda{
        sum("column name")}
    

    should work

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

Sidebar

Related Questions

I have some models like these: class Alpha < ActiveRecord::Base has_many :items end class
I have a the following rails models: class Release < ActiveRecord::Base has_many :release_questionnaires, :dependent
Rails 3.0.3 and Ruby 1.9.2 I have the following class class item < ActiveRecord::Base
I have an Item model class Item < ActiveRecord:Base has_and_belongs_to_many :orders end And an
Ok so I have this relationship in Rails: class Position < ActiveRecord::Base belongs_to :company
In a Rails app I have Sales and Salespeople: class Sale < ActiveRecord::Base belongs_to
I have a model: class Item < ActiveRecord::Base Item has a property 'store' Based
From the book Agile Web Development With Rails class Order < ActiveRecord::Base named_scope :last_n_days,
In my Rails 2.3.2 app I have 2 models: class Post has_many :approved_comments, :class_name
my ruby (on rails) class looks like: class Foo def self.method1 someAction end def

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.