Search code examples
ruby-on-railsimpressionist

rails impressionist how to find 5 of most view item


I using this instance to find more view 5 Item, but cannot get the result.
Can teach me how to get it?

 @test = Item.limit(5).joins(:impressions).group("impressions.impressionable_id").order("count(impressions.impressionable_id) DESC")

Solution

  • You can achieve the using counter cache. In this approach you've to create a new column to your model and add some configuration to your model. Thus, the gem will automatically update the view count.

    Or, (not tested, but should work)

    Item.joins(:impressions).group("impressions.impressionable_id").order("count(impression‌​s.id) DESC").first(5)