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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:46:44+00:00 2026-06-12T20:46:44+00:00

I have a Project model and a Developer model. I have the concept of

  • 0

I have a Project model and a Developer model. I have the concept of calculating the “interestingness” for a project for a particular developer:

class Project < ActiveRecord::Base
  def interestingness_for(developer)
    some_integer_based_on_some_calculations
  end
end

I think it would be neat, instead of having something like Project.order_by_interestingness_for(bill), to be able to say

Project.order(:interestingness, :developer => bill)

and have it be a scope as opposed to just a function, so I can do stuff like

Project.order(:interestingness, :developer => bill).limit(10)

I don’t know how to do this, though, because it’s not obvious to me how to override a scope. Any advice?

  • 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-12T20:46:46+00:00Added an answer on June 12, 2026 at 8:46 pm

    Assuming you will not need to use the standard ActiveRecord order query method for the Project class, you can override it like any other class method:

    def self.order(type, options)
      self.send(:"special_#{type}_calculation_via_scopes", options)
    end
    

    Then the trick is to ensure you create the needed calculation methods (which will vary according to your interestingness and other algorithms). And that the calculation methods only use scopes or other AR query interface methods. If you aren’t comfortable converting the method logic to a SQL equivalent using the query interface, you can try using the Squeel DSL gem which can potentially work with the method directly depending on your specific calculation.

    If you may be needing the classic order method (and this is usually a safe assumption), then don’t override it. Either create a proxy non-ActiveRecord object for this purpose, or use a different naming convention.

    If you really want to, you can use aliasing to achieve a similar effect, but it may have unintended consequences for the long term if the second argument (‘options’ in this case) suddenly takes on another meaning as Rails progresses. Here is an example of what you can use:

    def self.order_with_options(type, options = nil)
      if options.nil?
        order_without_options(type)
      else
        self.send(:"special_#{type}_calculation_via_scopes", options)
      end
    end    
    class << self
      alias_method_chain :order, :options
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this Project model: class Project < ActiveRecord::Base validates :status, :inclusion => {
I have a Project model similar to: class Project(models.Model): ... lead_analyst=models.ForeignKey(User, related_name='lead_analyst') ... I
I have the following model in a project: class CarAssignment(models.Model): leg = models.ForeignKey(Leg, null=True,
I have a Project model and it has some text attributes, one is summary.
I have a project which exposes object model to use by different types of
I have a M2M field in my django model project. In my view I
In my project I have a model where I use Imagekit to process an
I have a Java project that I'm trying to implement with a model-view-controller design.
I have a Python project with following directory structure: /(some files) /model/(python files) /tools/(more
I have an existing model for my project. I would like to switch to

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.