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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:10:22+00:00 2026-05-14T06:10:22+00:00

I have created a table in my Ruby on Rails application that I am

  • 0

I have created a table in my Ruby on Rails application that I am building called Tags. It is a blog application so I allow the user to associate tags with a post and do this through a :posts, :has_many => tags and Tag belongs_to :post association.

Now that I have my Tags table I am trying to see how I would render the view such that it displays the tag and tag count. (it should be noted that I am trying to render this in the /views/posts/index.html.erb file).

For instance, if there are 10 entries in the Tag table for tag_name Sports. How can I display Sports (10) in the view. I am not looking to do this for a specific tag but rather, somehow search the table, combine like tags and display a list of all tags with counts next to them. (I really want these to be a link to a list of posts that contain that tag but I learned early on only to ask one question at a time).

Hope that makes sense.

UPDATE FOR COMMENTS

view

<% @tag_counts.each do |tag_name, tag_count| %>
    <tr>
      <td><%= link_to(tag_name, posts_path(:tag_name => tag_name)) %> </td>
      <td>(<%=tag_count%>)</td>
    </tr>
<% end %>

postsController:

def index
    @tag_counts = Tag.count(:group => :tag_name)
    @posts = Post.all :order => "created_at DESC"



    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @posts }
      format.json { render :json => @posts }
      format.atom
    end
  end
  • 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-05-14T06:10:23+00:00Added an answer on May 14, 2026 at 6:10 am

    Do the following:

    Tag.count(:group => :name).each do |tag_name, tag_count|
      puts "tag_name=#{tag_name}, tag_count=#{tag_count}"
    end
    

    You might improve the performance if you add an index on thename column in the tags table.

    To display the posts associated with a tag name do the following:

    In the controller method set the count hash:

    Set @tag_counts in the controller action associated with the view displaying the tag names.

    @tag_counts = Tag.count(:group => :tag_name)
    

    In the view show each tag as a link:

    <% @tag_counts.each do |tag_name, tag_count| %>
      <%= link_to(tag_name, posts_path(:tag_name => tag_name)) %> (<%=tag_count%>)
    <% end %>
    

    The link points to the index method of your PostsController. Each link has a tag_name parameter.

    In the index method of PostsController:

    class PostsController < ApplicationController
      def index
        @tag_counts = Tag.count(:group => :tag_name)
        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)
      end
    end
    

    Edit
    Updated the code to change the name field to tag_name.

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

Sidebar

Related Questions

I have created a blog application using Ruby on Rails and have just added
I have just installed paperclip into my ruby on rails blog application. Everything is
I have built a blog application w/ ruby on rails and I am trying
I have built a blog application using Ruby on Rails. In the application I
I have an application using Ruby on Rails where a user will be providing
I have created a table in Visual Studio. That table has two columns. First
We have created a table with a trigger that updates a ModifiedDate field in
I am considering developing an application using Ruby on Rails that is a planner
First I'm quite new to ruby and rails... I have a little application working
I have an Ruby on Rails 3 Heroku application, which needs to perform text

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.