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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:04:53+00:00 2026-05-27T05:04:53+00:00

I have a method that will provide an array of model object. Some of

  • 0

I have a method that will provide an array of model object. Some of those model’s attributes are easy to sort by using some help from SQL. I mean, using .find(:condition => {})

The problem is some of the other attributes isn’t. It has to be calculated, modified, or did something else before showing. Then, I found the way to sort it using collection sorting. Which is, collection.sort{|a,b| a.something_to_sort <=> b.something_to_sort}

OK! That’s works. But the problem is, is it possible to make that something_to_sort part to become a dynamic variable? For example, I want to take a parameter from the UI and assign it to that something_to_sort like following,

HTML

<select name="sort">
    <option value="name">Name</option>
    <option value="age">Age</option>
    <option value="activity.category">Activity</option>
    <option value="tax.calculate_personal_income_tax">Income Tax</option>
    <option value="tax.calculate_withholding_tax">Withholding Tax</option>
    <option value="doctor.name">Doctor's Name</option>
</select>

Rails

params[:sort] ||= "Age"
@people = Person.find(:all)
@people.sort{|a,b| a.params[:sort] <=> b.params[:sort]}    #Note that this is an incorrect syntax

Is there any way to do this by not having to write a sort block for each of sorting option?

Additional #1

I’ve just tried this and it was working for some sorting option

@people.sort{|a,b| a.send(params[:sort]) <=> b.send(params[:sort])}

This works for name and age as they are people’s attribute (and it works for peopls’s method too). But for the association’s such as tax.calculate_personal_income_tax, it’s not.

So, my colleague told me to create new people’s method, calculate_personal_income_tax and then change the option value from tax.calculate_personal_income_tax to calculate_personal_income_tax so that the send method will be able to find this method …

def calculate_personal_income_tax
    tax.calculate_personal_income_tax
end

But, this is not really what I want. Because I believe that there must be a way to access this association method without having to define a new model method. If I choose to do this and one day people model become larger, more attribute, more information to sort and display. Then, there will be a lot of method like this.

The other reason is, if I have to define a new method, that method should have to do some logic, calculation or else, not just retrieve a data from other model.

Additional #2

Finally, I’ve just found the way to access other model attribute by adding args to the find method, :join and :select.

@people = Person.find(:all, :select => "persons.*, tax.tax_rate AS tax_rate", :joins => :tax, :condition => {some conditions})

The result of that will be a table of person joining with tax. Then, I use the send(params[:sort]) thing to help me sorting the attribute that I want.

  • 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-27T05:04:54+00:00Added an answer on May 27, 2026 at 5:04 am
    @people.sort {|a, b| a.send(params[:sort]) <=> b.send(params[:sort])}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code in method C that will get executed based upon the
We have a COM object implemented with C++/ATL that includes a method which will
I have a method that will receive a string , but before I can
Question Is there a way to have a method that will always run anytime
I have a method that I will use in the following contexts: 1. User
We have a static method in a utility class that will download a file
I need a Regex that will match a java method declaration. I have come
I’m after some C# code that will have the following methods that will return
I'm building a class library that will have some public & private methods. I
I have a custom object that contains an array (called children) where objects of

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.