Open-source project
ankane/hightop avatar
ankane/hightop

Hightop for ranked group counts in Ruby

A nice shortcut for group count queries

511 stars8 forksRubyMIT

At a glance

What is it?
Hightop gives Ruby applications a short top method for questions such as which browsers account for the most visits. It runs database aggregations for Active Record and Mongoid and counts Ruby collections in memory.
Who is it for?
Use Hightop when several Ruby reports repeat the same ranked count, and keep bespoke reporting queries explicit. Before adopting it, check the Ruby and Active Record versions and decide whether the report should count visit rows or distinct visitors.
Can I use it commercially?
Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 105 days ago.
What is it written in?
Mainly Ruby, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on October 20, 2026, and from our analysis. They are not legal advice.

Editorial analysis

Count browser visits with Active Record

Add the dependency to your application's Gemfile. Hightop 1.0.0 requires Ruby 3.3 or later and declares Active Support 7.2 or later. The changelog also records the end of support for Active Record versions below 7.2.

ruby
gem "hightop"

After resolving the dependency, use the README's example in an application that already has a Visit model and a browser attribute. The result maps browser values to counts ordered from highest to lowest.

ruby
Visit.top(:browser)

Choose what the count measures

The Active Record implementation builds a grouped relation, excludes null grouping values by default, and delegates the grouped count to Active Record. With a minimum, it uses a HAVING condition. A distinct attribute changes the count from matching rows to distinct values of that attribute, so multiple visits by the same user in one city contribute once to that city's distinct user count.

ruby
Visit.top(:city, distinct: :user_id)

Set a threshold before ranking

The two numeric controls answer different questions. A limit caps the number of groups returned. A minimum removes groups with too few observations before the report displays them. A referrer report with a limit of ten can still include groups with a single visit. A city report with a minimum of ten can return any number of cities with at least ten visits. If no city meets the threshold, there is no group to display. These controls are independent in the Active Record implementation, which adds a relation limit separately from the HAVING predicate.

The README shows the minimum on a city count. This is a count of matching rows per city because the call omits the distinct option.

ruby
Visit.top(:city, min: 10)

Decide what missing information means before using the resulting counts in a dashboard. Excluding visits without a search keyword is reasonable for a table of recorded keywords. A report on the completeness of visit records needs to retain that missing group. By default, the Active Record implementation adds a non-null condition for each grouping expression. With city and browser grouped together, that means a row with a known browser and a missing city is dropped along with rows whose browser is missing.

Grouping by several attributes also changes what a row in the report represents. The city and browser example makes each combination a separate bucket. It can answer which combinations occur most often. A team building a chart of each browser's share within its city would still need a per-city denominator. Likewise, a global result cap on combinations does not select the leading browser separately for every city. If that per-city ranking is the main requirement, write the reporting query around that requirement directly.

Choose the aggregation path

The Mongoid implementation builds an aggregation pipeline. With distinct counting, it groups by the selected keys plus the distinct field, then groups again using only the selected keys. On ordinary Ruby collections, Hightop adds top to Enumerable and builds a frequency hash, sorts its entries by count and then applies the result limit. A small result limit still requires visiting the collection and collecting the counts. The hash example below selects the value from each key-value pair.

ruby
{a: "up", b: "up", c: "down"}.top { |k, v| v }

For a collection already held in memory, the block is a convenient way to choose the value to count. The implementation maps the entries before tallying them, so the block runs even for values that will be absent from the final result. The count hash also grows with the number of different values encountered. For a report on an existing database table, first decide whether the values can be grouped in the database. Loading those records into Ruby just to apply the collection form would move the tally and its intermediate data into the application process.

When to keep the query explicit

A direct Active Record group and count gives maintainers control over the ordering and aggregate rules at the call site. That is useful for a report that needs a business-defined ordering for equal counts or several aggregate columns. Hightop is a better fit when the recurring report is a ranked frequency table. The project ships under the MIT license, whose notice calls for retaining the copyright and permission text in copies or substantial portions. The project README, implementation, changelog and LICENSE.txt are available in the [upstream repository](https://github.com/ankane/hightop).

An upgrade also deserves a check of existing call sites that rely on result order. The Active Record implementation adds the grouping columns to its count ordering. The Mongoid pipeline explicitly sorts by count, and the Ruby collection implementation sorts by the negative count. If two groups tie for the final position in a limited report, write a test for the report's desired choice before relying on the result across those backends. For a small report, fixtures with equal counts and missing group attributes exercise the two decisions that are easiest to overlook.

Editorial conclusion

Use Hightop when several Ruby reports repeat the same ranked count, and keep bespoke reporting queries explicit. Before adopting it, check the Ruby and Active Record versions and decide whether the report should count visit rows or distinct visitors.

Frequently asked questions

Can Hightop include missing group values?

Yes. The README shows passing the nil option with a value of true to include missing grouping values.

Can Hightop group by city and browser together?

Yes. The README passes an array containing city and browser to group by both attributes.

Does Hightop work with Ruby arrays?

Yes. The README shows counting repeated values in an array, with options for a result limit and minimum count.

Official sources

  1. ankane/hightop on GitHub
  2. Issues
  3. License: MIT
  4. README

Was this useful?