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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:59:46+00:00 2026-05-21T04:59:46+00:00

I want to select users from the users table based on: user.created user.sales_count So

  • 0

I want to select users from the users table based on:

user.created
user.sales_count

So I want to fetch all users, sometimes ordered by created date, and sometimes based on sales_count. And I want to be able to switch between ASC or DESC order.

All queries need to have this WHERE clause:

WHERE region = 123

How can I build my active record query to support these order by conditions?

  • 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-21T04:59:47+00:00Added an answer on May 21, 2026 at 4:59 am
    def get_users(options={})
      options[:order_col] ||= "created"
      options[:order_type] ||= ""
      User.where(:region=>123).order("#{options[:order_col]} #{options[:order_type]}")
    end
    

    options[:order_col] ||= is really saying:

    options[:order_col] = options[:order_col] || ""

    which in english is saying set options[:order_col] to options[:order_col] if set, other wise "". We can set the order_type to "" because SQL will by default order results ASC.

    Example:

    get_users #=> return ordered by created ASC
    get_users(:order_col => "sales_count") #=> return order by sales count ASC
    get_users(:order_col => "sales_count", :order_type => "DESC") #=> sales_count, DESC
    # etc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have users table and i have posts table i want select from users
I have a user table from which I want all values, so I have
I want to do SELECT count(*) FROM users where created_at within 3 days ago
I want to select a view from my sys user, but with other connection.
In my application, i want user to select a font, from a list of
I want to enable the user to select a video file from the on-device
I have users table and want to SELECT some rows by bitmask criteria. I'll
All I want to count entries based on date.(i.e entries with same date.) My
I want to filter a table using jQuery hide/show based on what the user
I want to select the Resources that are used by the most users. The

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.