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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:51:00+00:00 2026-05-10T18:51:00+00:00

I am using running a simple find all and paginating with willpaginate, but I’d

  • 0

I am using running a simple find all and paginating with willpaginate, but I’d also like to have the query sorted by the user. The first solution that came to mind was just use a params[:sort]

http://localhost:3000/posts/?sort=created_at+DESC  @posts = Post.paginate :page => params[:page], :order => params[:sort] 

But the problem with his approach is that the query is defaulting as sorting by ID and I want it to be created_at.

Is this a safe approach to sorting and is there a way to default to created_at?

  • 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. 2026-05-10T18:51:00+00:00Added an answer on May 10, 2026 at 6:51 pm

    I’d use a named scope for providing the default order (available since Rails 2.1).

    You’d add the scope in your Post model:

    named_scope :ordered, lambda {|*args| {:order => (args.first || 'created_at DESC')} } 

    Then you can call:

    @posts = Post.ordered.paginate :page => params[:page] 

    The example above will use the default order from the named_scope (created_at DESC), but you can also provide a different one:

    @posts = Post.ordered('title ASC').paginate :page => params[:page] 

    You could use that with Romulo’s suggestion:

    sort_params = { 'by_date' => 'created_at', 'by_name' => 'name' } @posts = Post.ordered(sort_params[params[:sort]]).paginate :page => params[:page] 

    If params[:sort] isn’t found in sort_params and returns nil then named_scope will fall back to using the default order.

    Railscasts has some great info on named_scopes.

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

Sidebar

Related Questions

I have an extremely simple web application running in Tomcat using Spring 3.0.2, Hibernate
So I'm trying to do a simple insert using Linq but i'm running into
I have a simple java program that runs fine in eclipse but cannot find
I have been searching for an answer to this but cannot find a simple
I am currently running a simple EJB application using a stateless Session Bean. I
I am running some import code asynchronously from a simple WinForms app using a
I'm running a java webapp with a simple mvn jetty:run , using the latest
I have two IIS servers running using NLB. Unfortunatelly I cannot use shared session
I have a simple model setup in my Ruby on Rails app. (User {name,
I really need help on getting a simple GTK program in c++ running using

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.