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.
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.
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
<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 ...