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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:51:40+00:00 2026-06-01T21:51:40+00:00

this will show up in the sidebar, that is why i put it in

  • 0

this will show up in the sidebar, that is why i put it in application controller. im open to a better solution

class ApplicationController < ActionController::Base

  protect_from_forgery

  before_filter :get_tags_latest, :get_tags_popular

  def get_tags_latest
    @tags_latest = Tag.all.first(5)
  end

  def get_tags_popular
    @tags_popular = Tag.by_most_resources.limit(10)
  end

end

tag.rb:

class Tag < ActiveRecord::Base

  self.include_root_in_json = false

  has_many :resource_tags
  has_many :resources, :through => :resource_tags

  attr_accessible :name

  validates :name, :presence => true,
                   :length   => { :within => 2..20 },
                   :uniqueness => { :case_sensitive => false }

  scope :by_most_resources,
    joins("INNER JOIN resources ON resources.tag_id = tags.id").
    group("tags.*").order("count(resources.id) DESC")

end

sidebar.html.erb

<ul class="tag-list">
    <% @tags_popular.each do |t| %>
      <li><%= link_to t.name, tag_path(t), :class => :tag %> (<%= t.resources.count %>)</li>
    <% end %>
  </ul>

I don’t have much code at the moment (hopefully its in the right spot too)… really all I want to do is show the most popular 10 tags sorted by tag.resources.count, as well as the latest 5 tags sorted by date. I tried looking around for find(:order => ) but that proved unhelpful.

Is there a magical way to do this? 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-06-01T21:51:41+00:00Added an answer on June 1, 2026 at 9:51 pm

    Starting with the SQL

    SELECT tags.*
    FROM tags
      INNER JOIN resources ON resources.tag_id = tags.id
    GROUP BY tags.*
    ORDER BY count(resources.id) DESC
    LIMIT 10
    

    so, to ActiveRecordize this…

    class Tag < ActiveRecord::Base
      scope :by_most_resources,
        joins("INNER JOIN resources ON resources.tag_id = tags.id").
        group("tags.*").order("count(resources.id) DESC")
    

    call this by:

    Tag.by_most_resources.limit(10)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a notification area that will show alerts. return this.each(function() {
For this question, let us assume that we will want to show the face
<div style=background-color:black> <div style=float:right> Test message </div> <div> This will show 'Test message' with
from this code will show in original color how can i change it to
This will require a little setup. Trust me that this is for a good
The official Git doc says: $ git diff test This will show you what
At some angles this will show an intersection on the wrong side (back when
I am trying to create a sidebar for a site that will allow a
I have the following fiddle here: http://jsfiddle.net/9jN8L/ The idea is that sidebar will be
this is the code ,this will show the default size, i want to change

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.