Ordering view by highest group count question - Ruby on Rails
Posted
by bgadoci
on Stack Overflow
See other posts from Stack Overflow
or by bgadoci
Published on 2010-04-16T04:29:10Z
Indexed on
2010/04/16
4:33 UTC
Read the original article
Hit count: 431
I've read the couple of questions about this on stack overflow but can't seem to find the answer. I am trying to display the tags in my blog by the ones with the highest count in the tags table.
Thanks to KandadaBoggu for helping me get the tags feature of the blog I am designing working. Here is the basics and my question.
Tag belongs_to :post and Post has_many :tags. The tags table is simple really, consisting of the normal scaffolded fields plus post_id and tag_name (I actually called the column 'tag_name' instead of just 'name').
in my /views/posts/index.html/erb file I correctly am displaying the tags by group and the amount of times they are being used (appearing in the tags table). I just want to know how to order them by the highest count. Here is the code, and I currently have it set to updated_at:
PostsController
def index
@tag_counts = Tag.count(:group => :tag_name, :order => 'updated_at DESC', :limit => 10)
conditions, joins = {}, nil
unless(params[:tag_name] || "").empty?
conditions = ["tags.tag_name = ? ", params[:tag_name]]
joins = :tags
end
@posts=Post.all(:joins => joins, :conditions=> conditions, :order => 'created_at DESC').paginate :page => params[:page], :per_page => 5
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
format.json { render :json => @posts }
format.atom
end
end
© Stack Overflow or respective owner