The perfect way to get random record in Rails, the most perfomant!

Hey everyone :waving_hand:

I’ve just released random-rails, a new Ruby gem that provides a blazing-fast way to get random records in Rails.

Unlike common approaches that rely on ORDER BY RANDOM() or large offsets, this gem is optimized specifically for PostgreSQL and designed for perfect performance even on large datasets.

It’s simple, lightweight, and works seamlessly with ActiveRecord scopes.

Check it out here: https://github.com/the-rubies-way/random-rails

I’d love to hear your feedback, ideas, and benchmarks from real-world usage!

3 Likes

Cool, clean, easy to understand gem, I learned few things from reading the source.

I’d suggest emitting a warning here random-rails/lib/random-rails/adapters/active_record/base.rb at 85111c60ad4200adad4a7f8056cde0903055fa25 · the-rubies-way/random-rails · GitHub

Because if someone is requesting a strategy that’s not actually used (because they’re not using postgres) then something is happening they didn’t expect.

In my projects I tend to be more radical, and raise in such cases. But I never was a custodian of a gem, so I’m hesitant to suggest that.