How do I optimize rails scope to get the correct results?

Discussion in 'Web Design' started by RechardSmith, Sep 10, 2017.

  1. RechardSmith

    RechardSmith Registered Member

    Joined:
    Sep 10, 2017
    Messages:
    6
    Likes Received:
    0
    I would like to show my 5 most active tags.

    I did the following but I have a feeling the results are not correct.

    Code:
    has_many :taggings,  :dependent => :delete_all
    has_many :posts, through: :taggings
    scope :top5, ->  {
            self.select("tags.id, tags.name, count(taggings.id) as count").joins(:taggings).group("tags.id").limit(5).order('count DESC')
        }
    Does this seem correct? I am using pg.
     

Share This Page