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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:14:02+00:00 2026-05-31T10:14:02+00:00

So basically I have two classes, Book and Author . Books can have multiple

  • 0

So basically I have two classes, Book and Author. Books can have multiple authors and authors can have multiple books. Books have the following default scope.

default_scope :order => "publish_at DESC"

On the Author show page I want to list all the books associated with that author so I say the following…

@author = Author.find(params[:id])
@books = @author.books

All is good so far. The author#show page lists all books belonging to that author ordered by publication date.

I’m also working on a gem that is able to sort by the popularity of a book.

@books = @author.books.sort_by_popularity

The problem is that whenever it tries to sort, the default_scope always gets in the way. And if I try to unscope it before it will get rid of the author relation and return every book in the database. For example

@books = @author.books.unscoped.sort_by_popularity # returns all books in database

I’m wondering if I can use the ActiveRelation except() method
to do something like this (which seems like it should work but it doesn’t. It ignores order, just not when it is a default_scope order)

def sort_by_popularity
  self.except(:order).do_some_joining_magic.order('popularity ASC')
  #    |------------|                       |---------------------|
end

Any ideas as to why this doesn’t work? Any ideas on how to get this to work a different way? I know I can just get rid of the default_scope but I’m wondering if there another way to do this.

  • 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-31T10:14:03+00:00Added an answer on May 31, 2026 at 10:14 am

    You should be able to use reorder to completely replace the existing ORDER BY:

    reorder(*args)
    Replaces any existing order defined on the relation with the specified order.

    So something like this:

    def self.sort_by_popularity
      scoped.do_some_joining_magic.reorder('popularity ASC')
    end
    

    And I think you want to use a class method for that and scoped instead of self but I don’t know the whole context so maybe I’m wrong.

    I don’t know why except doesn’t work. The default_scope seems to get applied at the end (sort of) rather than the beginning but I haven’t looked into it that much.

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

Sidebar

Related Questions

I have basically two questions. How do I locate the default Rprofile which is
I have a web site with two routes. Basically the user can do one
I have an interesting issue today!! Basically I have two classes. public class A
I have two classes Student and Tutor. Tutor is basically a student (Tutor extends
I have a question about JAXB. Basically what I have are these two classes:
Assume I have the following two classes: public class User : Entity { public
I have two classes, basically one holds Members and the other Sessions. They are
Basically, I have two classes : - MyActivity.java - OtherClass.java Overview of MyActivity.java :
Basically I have two classes Inventory and Character . During the construct of the
If I were to select a row from a table I basically have two

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.