[rails] tag ranking function

I added a tagging feature to my portfolio today and created a ranking Output.

The tagging function used a form object. First, write this in the tweet controller.

tweets_controller.rb


def rank
   @tag_ranks = TweetTag.find( TweetTagRelation.group(:tweet_tag_id).order('count(tweet_tag_id)desc').limit(4).pluck(:tweet_tag_id))
 end

I wondered what pluck was ... so I looked it up. Officially pluck can be used to send a query to retrieve columns (s) from the table used in one model. Given a list of column names as an argument, returns an array of values for the specified column with the corresponding data type.

That's right.

You can get the contents by taking tweet_tag_id (this is the tag column of the intermediate table) as the argument of pluck.

Next is view Create a file under tweets and write it like this

rank.html.erb


<div class="rank-tag-all">
        <% @tag_ranks.each.with_index(1) do |tag,i| %>
          <div class="rank-tag-num">No.<%= i %>Rank</div> 
          <div class="rank-tag"><%= tag.name%></div>
        <%end%>
</div>

The ranking is displayed by writing with_index (1) and taking the second argument.

Chat I was pretty sleepy so I scratched it simply. .. .. I plan to write qiita earlier ...

Recommended Posts

[rails] tag ranking function
[Rails 6] Ranking function
[Rails] Tag management function (using acts-as-taggable-on)
[Rails] Category function
Rails follow function
Rails linked_to tag
[Rails] Notification function
[Rails] Implement search function
[Rails] Implemented hashtag function
Rails search function implementation
[Ruby on Rails] Posting score ranking function (whole display)
Implement application function in Rails
Rails fuzzy search function implementation
[Rails] Implement User search function
Introduced graph function with rails
Search function using [rails] ransack
Implement follow function in Rails
[Rails] Implementation of category function
Rails ~ Understanding the message function ~
[Rails] (Supplement) Implemented follow function
Login function implementation with rails
[Rails] EC site cart function
Ajax bookmark function using Rails
[Rails] Implementation of tutorial function
[Rails] Implement image posting function
[Rails] Implementation of like function
[Rails 6] Pagination function implementation (kaminari)
[Rails] Implementation of CSV import function
Add a search function in Rails.
[Rails] About the Punk List function
[Ruby on Rails] Introduced paging function
[Rails] Implementation of image preview function
Implemented mail sending function with rails
Kaminari --Added pagination function of Rails
[Rails] About implementation of like function
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
Create pagination function with Rails Kaminari
Implement simple login function in Rails
[Rails] Voice posting function ~ Cloudinary, CarrierWave
[Rails] Comment function (registration / display / deletion)
[Rails] How to put a crown mark on the ranking function
[Rails] gem ancestry category function implementation
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
[Rails 6] Like function (synchronous → asynchronous) implementation
Implement CSV download function in Rails
[Rails] Comment function implementation procedure memo
[Rails] Implementation of tag function using acts-as-taggable-on and tag input completion function using tag-it
[Ruby on Rails] Search function (not selected)
[Rails] Addition of Ruby On Rails comment function
[Rails] Function restrictions in devise (login / logout)
[Rails withdrawal] Create a simple withdrawal function with rails
[Ruby on Rails] Follow function implementation: Bidirectional
Rails [For beginners] Implementation of comment function
Make a login function with Rails anyway
Rails Basic CRUD function implementation procedure scaffold
Implemented follow function in Rails (Ajax communication)
[Rails 6] Implementation of SNS (Twitter) sharing function
How to implement ranking functionality in Rails
[Vue.js] Implementation of menu function Implementation version rails6