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

  • Home
  • SEARCH
  • 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 998325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:10:33+00:00 2026-05-16T07:10:33+00:00

I have set up two models: user and post . Each post belongs_to a

  • 0

I have set up two models: user and post. Each post belongs_to a user. posts also have tags, using acts_as_taggable. On UserController#show I want to list the tags the user uses, sorting from most used to less used.

Getting a list of tags is not hard, but how can I sort them? I use this to find the tags:

@tags = []
@user.posts.each do |post|
  @tags += post.tags
end

Can anyone explain me how I can sort the tags? Thanks.

  • 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-16T07:10:33+00:00Added an answer on May 16, 2026 at 7:10 am

    you could use tag_counts method provided by plugin:

    @user.posts.tag_counts
    

    more info here: http://agilewebdevelopment.com/plugins/acts_as_taggable_on_steroids

    EDIT:

    a basic simple code for sorting:

    @tags = Hash.new(0)
    @user.posts.each do |post|
      post.tags.each do |tag|
        @tags[tag] += 1 if @tags.has_key?(tag)
      end
    end
    # sorting
    @tags.sort{|a,b| a[1] <=> b[1]}
    

    maybe there’s a better way of doing it.

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

Sidebar

Related Questions

In a Rails 3.2 app I have two user models set up using Devise:
I have a two models set up like this: class User < ActiveRecord::Base #
I have two models and a join table, User , Post and posts_users .
I have a two models, Group and User User belongs_to Group and Group has_many
I have two Models called User and Membership. User has_many :memberships Membership belongs_to :user
So I have two rails models - user and role - each specified with
I have two models: Company and User they have a has_and_belongs_to_many relationship. I'm using
I have a question about how to set up the relations between two models
Possible Duplicate: Retrieving the last record in each group I have two tables set
DUPLICATE: Using a Django custom model method property in order_by() I have two models;

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.