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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:41:30+00:00 2026-06-17T12:41:30+00:00

When you have a partial in /app/views/posts/comments/_comment.html.erb , doing the following <%= render @post.comments

  • 0

When you have a partial in

/app/views/posts/comments/_comment.html.erb,

doing the following

<%= render @post.comments %>

iterates the @post.comments collection like

@post.comments.each do |comment|

automatically, even if you don’t pass in the collection into the partial.

However, this will render the comments in the other direction, because of the way objects are sorted by created_at.

I want to change the direction: sort the collection in the other way by created_at DESC, and then iterate the comments collection.

I would do

@comments = @post.comments.paginate(:page => params[:page], :per_page => 10, :order => "created_at") in the PostsContoller and <%= render @comments %> instead of <%= @post.comments %>, but I was curious if there were any more common way of doing this.

Thanks in advance!

  • 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-17T12:41:30+00:00Added an answer on June 17, 2026 at 12:41 pm

    You definitely want to do:

    @comments = @post.comments.order("created_at ASC").paginate(page: params[:page])
    

    instead of changing how a Rails helper works! Why would you do that?

    You can define how comments are ordered on the association. In the Post model:

    has_many :comments, order: "comments.created_at ASC"
    

    You can also set a default scope to change how the comments are ordered by default.

    In the model:

    default_scope order('created_at ASC')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have in my app/views/bar/index.html.haml the following %p= render partial: 'foo', collection: @foos the
So, I have this code in a partial called _form.html.erb in app/views/posts : <%=
I have a form for creating an album like following: # app/views/albums/new.html.erb <h2>Details of
from my view workshop/app/views/administration/dashboards/show.html.haml I render the partial = render :partial => administration/dasboards/activity_list but
I have an MVC app with a Partial View (below). When I add Html.EnableClientValidation(),
I have a partial page in my application named _dashboard.html.erb. Inside of the dashboard,
I have the following code in my controller: File: app/controllers/photos_controller.rb def show_snapshot_comments snapshot =
I have the following form with Formtastic: = semantic_form_for @user, :url=> url_for(register_path), :html =>
In a Rails 3 blog type of app, I have a polymorphic comments model,
I have the following routes in my 3.1.0.rc5 app # config/routes.rb devise_for :users resources

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.