The reverse order of displaying blog posts and comments, Ruby on Rails

I am new to rails, so I could help here. I followed several tutorials to create a blog with comments and even some of the AJAX bells and whistles, and I was stuck on something that I hope is easy. The default display for both blogs and comments is a list of the oldest. How do I undo this to display the most recent posts and the most recent comments at the top. I do not know if this is a function of the controller or the model. I did some tweaking, so here is the code for the controller .rb files.

COMMENTS CONTROLLER

class CommentsController < ApplicationController def create @post = Post.find(params[:post_id]) @comment = @post.comments.create!(params[:comment]) respond_to do |format| format.html { redirect_to @post} format.js end end end 

POSTS CONTROLLER

 class PostsController < ApplicationController before_filter :authenticate, :except => [:index, :show] # GET /posts # GET /posts.xml def index @posts = Post.all(:include => :comments) respond_to do |format| format.html # index.html.erb format.xml { render :xml => @posts } format.json { render :json => @posts } format.atom end end # GET /posts/1 # GET /posts/1.xml def show @post = Post.find(params[:id]) respond_to do |format| format.html # show.html.erb format.xml { render :xml => @post } end end # GET /posts/new # GET /posts/new.xml def new @post = Post.new respond_to do |format| format.html { redirect_to @post} format.js end end # GET /posts/1/edit def edit @post = Post.find(params[:id]) end # POST /posts # POST /posts.xml def create @post = Post.new(params[:post]) respond_to do |format| if @post.save flash[:notice] = 'Post was successfully created.' format.html { redirect_to(@post) } format.xml { render :xml => @post, :status => :created, :location => @post } else format.html { render :action => "new" } format.xml { render :xml => @post.errors, :status => :unprocessable_entity } end end end # PUT /posts/1 # PUT /posts/1.xml def update @post = Post.find(params[:id]) respond_to do |format| if @post.update_attributes(params[:post]) flash[:notice] = 'Post was successfully updated.' format.html { redirect_to(@post) } format.xml { head :ok } else format.html { render :action => "edit" } format.xml { render :xml => @post.errors, :status => :unprocessable_entity } end end end # DELETE /posts/1 # DELETE /posts/1.xml def destroy @post = Post.find(params[:id]) @post.destroy respond_to do |format| format.html { redirect_to(posts_url) } format.xml { head :ok } end end private def authenticate authenticate_or_request_with_http_basic do |name, password| name == "admin" && password == "secret" end end end 
+6
ruby ruby-on-rails controller model
source share
6 answers

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.

+19
source share

If you're looking for a more general way to reorder the .each method, Rails has a .reverse_each method. For example:

 <% @posts.reverse_each do |post| %> <%= render :partial => post %> <%= render :partial => post.comments.reverse %> <% end %> 
+6
source share
 @posts = Post.find(:all, :include => :comments, :order => "published_at DESC") 
+3
source share

It looks like you can reverse the order using find : something like Post.find(:all, :order => "created_at DESC") . The same goes for comments.

0
source share

.reverse_each bumping method with will_paginate

here is the solution

 @posts = Post.all.paginate(:order => "created_at DESC",:page => params[:page],:per_page => 5) 
0
source share

try using: reverse_order

 Client.where("orders_count > 10").order(:name).reverse_order 

this will execute SQL:

 SELECT * FROM clients WHERE orders_count > 10 ORDER BY name DESC 

If the order proposal is not specified in the request, reverse_order orders the primary key in reverse order.

 Client.where("orders_count > 10").reverse_order 

which will be executed:

 SELECT * FROM clients WHERE orders_count > 10 ORDER BY clients.id DESC 

http://edgeguides.rubyonrails.org/active_record_querying.html#reorder

0
source share

All Articles