[Feature Request] adding .sample to ActiveRecord

Hi there,

I’ve never contributed to Rails before, so if I’m doing something wrong, please point me in the right direction.

I often do something like User.all.sample in Rails console to get a random record. This is ok, but it loads every record from the db before calling sample to get a single record. Some projects have 100k+ users, so that takes some time.

I would like to contribute to Rails by adding a ActiveRecord::Scoping::Named::ClassMethods#sample method to retrieve a single (or more if an argument is specified) random record from the db.

I’ve been reading a couple of “how to contribute to rails” guides, one of them led me to this group. Is anyone able to guide me from here? How do I get approval for this feature?

Cheers,

Regan

One of the first things to do is check if this feature would not proposed before. I think you will see that it was already proposed and rejected.

See
https://github.com/rails/rails/pull/16997