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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:10:48+00:00 2026-05-20T22:10:48+00:00

Have simple Order – Item app. No cart. Need help with joined scopes for

  • 0

Have simple Order – Item app. No cart. Need help with joined scopes for my reports.

Item is polymorphic (belongs_to :itemable, :polymorphic => true) with Products, Services and Policies. Policies belongs to Vendor.

Goal is list out how much was sold by Vendor but still be able to use the scopes for Orders as seen below.

Scopes in my Orders model

scope :closed_today, lambda { where("closed_date IS NOT ? AND closed_date >= ? AND closed_date < ?", nil, Time.now.midnight, Time.now.midnight.tomorrow)}
scope :closed_yesterday, lambda { where("closed_date IS NOT ? AND closed_date >= ? AND closed_date < ?", nil, Time.now.midnight.yesterday, Time.now.midnight)}
...

Something like – Guess in Orders Model?

scope :with_policies, joins(:items).where(:itemable_type => "Policy")

And Final Result being able to group by vendor and get amounts sold.

<% for v in @orders.closed_today.with_policies.group("itemable.vendor_id") %>
  <%= v.somehow_get_to.vendor.name %> Sold: <%= v.somehow_get_to.collect.sum(&:price) %>

Price is the item field that holds how much that item sold for.

I know you can’t group as shown above, so any pointers on how to do that would be great. Would like to avoid adding vendor_id to my Items table.


UPDATE

Here is what I ended up doing.
Added vendor_id to items. Made my life easier. Sure this is a better way of doing this.

In my Items model

scope :in_orders_with, lambda { |orders, type| where('itemable_type IS ?', type).joins(:order) & orders }

In my view (since @orders.closed_today is actually passed in from another view.)

<% orders = @orders.closed_today %>
<p>
<% @items = Item.in_orders_with(orders, "Policy") %>
<% if !@items.blank? %>
  <% @items.group_by{ |o| o.vendor }.each do |vendor, items| %>
   <%= vendor.name %> <br />Qty Sold:<%= items.count %> Dollars Sold:<%= items.collect.sum(&:price) %><br /><br />
  <% end %>
<% else %>
  Nothing to report from Policies<br />
<% end %>
</p>
  • 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-20T22:10:48+00:00Added an answer on May 20, 2026 at 10:10 pm
    <% @orders.closed_today.with_policies.group_by{|o| o.itemable.vendor}.each do |vendor, orders| %>
      <%= vendor.name %> SOLD: <%= orders.sum(&:price) %>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly simple order creation form on a back office app I'm
I have a simple WCF service: public Order[] GetOrdersByStatus(int statusid) { OrderService os =
I have multiple ordered lists. Unfortunately, the order of the items isn't a simple
Suppose I have simple class Order, that have a TotalPrice calculated property, which can
I'm getting started with FubuMVC and I have a simple Customer -> Order relationship
Say I have a simple WCF application that the client calls in order to
I have simple task where I need to check objects in one dictionary and
Anyone have a good resource or provide a sample of a natural order sort
I have simple regex \.*\ for me its says select everything between and ,
Ok, i have simple scenario: have two pages: login and welcome pages. im using

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.