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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:22:31+00:00 2026-06-15T20:22:31+00:00

I don’t fully understand what rails scopes do. The rails query: UserDesign.joins(:order_line_items) .where(‘order_line_items.created_at >

  • 0

I don’t fully understand what rails scopes do. The rails query:

UserDesign.joins(:order_line_items)
     .where('order_line_items.created_at > ?',Date.today-1.month)
     .find(:all,
           :select=>'order_line_items.design_id as id,sum(order_line_items.quantity) as quantity',
           :group=>'order_line_items.design_id',
           :order=>'quantity desc'
     )

Returns the items:

 UserDesign Load (0.8ms)  SELECT order_line_items.design_id as id,sum(order_line_items.quantity) as quantity FROM "user_designs" INNER JOIN "order_line_items" ON "order_line_items"."design_id" = "user_designs"."id" WHERE (order_line_items.created_at > '2012-11-12') GROUP BY order_line_items.design_id ORDER BY quantity desc
+-----+----------+
| id  | quantity |
+-----+----------+
| 199 | 65       |
| 196 | 31       |
| 197 | 31       |
| 198 | 30       |
| 204 | 30       |
| 203 | 30       |
+-----+----------+

This is correct, but only two columns are included. The same query as a named scope yields:

UserDesign Load (0.7ms)  SELECT order_line_items.design_id as id,sum(order_line_items.quantity) as quantity FROM "user_designs" INNER JOIN "order_line_items" ON "order_line_items"."design_id" = "user_designs"."id" WHERE (order_line_items.created_at > '2012-11-12') GROUP BY order_line_items.design_id ORDER BY quantity desc
UserDesign Load (1.0ms)  SELECT "user_designs".* FROM "user_designs" 
+-----+-------+---
| id  | de... |...
+-----+-------+---
| 196 | aa... |...
| 199 | fd... |...
| 198 | as... |...
| 197 | as... |...
| 203 | Test  |...
| 204 | My... |...
+-----+-------+---

The SQL query is identical and all columns are returned, but they are not in the correct order. I would like to either

  1. Use a named scope that is ordered correctly
  2. wrap the plain query in a function but return all columns

Thanks!

EDIT:

For completeness, here’s the named scope:

scope :popular_this_month, lambda{
     joins(:order_line_items)
     .where('order_line_items.created_at > ?',Date.today-1.month)
     .find(:all,
           :select=>'order_line_items.design_id as id,sum(order_line_items.quantity) as quantity',
           :group=>'order_line_items.design_id',
           :order=>'quantity desc'
     )}
  • 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-06-15T20:22:33+00:00Added an answer on June 15, 2026 at 8:22 pm

    In the first case you’re passing a select option specifiying those 2 columns, so those two columns are what is returned. If you want more columns, list them (You can use table_name.* to get all the columns for a table)

    In your second case you’re calling find inside your named scope, which doesn’t make any sense – a named scope is just a set of conditions and options, you shouldn’t be actually doing the find in there. I suspect in that case it’s ignoring all the options you’re passing to find.

    You want something more along the lines of

    scope :popular_this_month, lambda{
         joins(:order_line_items).
         where('order_line_items.created_at > ?',Date.today-1.month).
         select(...).
         order(...).
         group(...)}
    

    which should result as the same behaviour as the first example.

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

Sidebar

Related Questions

Don't know how to google for such, but is there a way to query
I don't know why, but this code worked for me a month ago... maybe
Don't overlook the 'date AND TIME' part though.
Don't think that I'm mad, I understand how php works! That being said. I
Don't understand this simple code: def main(): print (This program illustrates a chaotic function)
don't understand: in my controller: @json = User.all.to_gmaps4rails do |user| \Title\: \#{user.email}\ end in
Don't understand, if Data.Map is and [] is. I found this out while wondering
Don't quite understand determinism in the context of concurrency and parallelism in Haskell. Some
Don't understand why #include <Header.h> is not compiling while #include Header.h is compiling with
Don't quite understand why this copy constructor is not invoked when I build with

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.