As jtbandes pointed out, in order to discard messages in an index, you must change the line in your index action to read:
@posts = Post.all(:include => :comments, :order => "created_at DESC")
There are two options to undo your comment list.
Option 1. In your message model, you can declare your relationship as follows:
class Post < ActiveRecord::Base has_many :comments, :order => "created_at DESC" end
Option 2: in your index view, simply undo the array of each comment before showing them:
<% @posts.each do |post| %> <%= render :partial => post %> <%= render :partial => post.comments.reverse %> <% end %>
Parameters have different use cases. In option 1, you say that throughout your application, each time you refer to comments on a message, these comments should be retrieved from the database in the specified order. You say that this is an integral property of comments in your application - there are many comments in the messages that, by default, order the newest first.
In option 2, you simply change the comments on the index page before they are displayed. They were still in the original order (the oldest of them) from the database, and they will still be displayed in that order at any place where you will receive comments on the message in your application.
PreciousBodilyFluids
source share