Katie presents a Ruby on Rails coding tip on selecting a random record from a database model in the TWIL series.

Welcome to another edition of TWIL, where our developers share fun-sized insights into software craftsmanship. This week, join Katie as she demonstrates a whimsical yet handy Ruby on Rails trick by unveiling a method to fetch a Random Record from Models. Learn how just a few lines added to ApplicationRecord can elevate your code's versatility, letting you pull a surprise entry from your datasets with Ruby's elegant simplicity.

Method on models to get random record

This may not have terribly many practical applications, but it's a pretty fun method that, by adding it to ApplicationRecord, allows you to get a random record of a given class (or query set) throughout your application.

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true

  def self.random(*q)
    queryset = q.any? ? self.where(*q) : self
    offset = rand(queryset.count) 
    queryset.offset(offset).first
  end
end

As an example:

User.all # Emily, Frank, Katie, Marisa, Micah, Andrea, Sara, Scott

# User.all (8)
User.random # Emily  == User.offset(0).first
User.random # Micah  == User.offset(4).first
User.random # Marisa == User.offset(3).first

# User.where(has_dog: true) (6: Emily, Frank, Katie, Micah, Andrea, Scott)
User.random(has_dog: true) # Katie  == User.where(has_dog: true).offset(2).first
User.random(has_dog: true) # Frank  == User.where(has_dog: true).offset(1).first
User.random(has_dog: true) # Andrea == User.where(has_dog: true).offset(4).first

# User.where(mean: true) (0)
User.random(mean: true) # nil == User.where(mean: true).offset(0...).first
  • Ruby
  • Rails
Katie Linero's profile picture
Katie Linero

Senior Software Engineer

Related Posts

AWS logo centered over dark blue stylized map of Europe with concentric radar-style rings emanating from Germany, representing the AWS European Sovereign Cloud infrastructure launch for EU data sovereignty and GDPR compliance
January 26, 2026 • Frank Valcarcel

AWS Launches European Sovereign Cloud

AWS launched a physically separate cloud infrastructure in Europe with EU-only governance, zero US dependencies, and over 90 services. Here is what organizations in healthcare, finance, and government need to know about the sovereign cloud and how to evaluate it for their compliance strategy.

Code snippet showing a Python Pydantic MovieReview model with typed fields (title, rating, summary, pros, cons) and OpenAI's response_format parameter for structured outputs, syntax highlighted on a dark editor background
November 12, 2025 • Frank Valcarcel

How to Get Guaranteed JSON from LLMs with Structured Outputs

Tired of parsing flaky JSON from LLM responses? OpenAI’s Structured Outputs feature guarantees your responses match your schema exactly. Here’s how to use it with Pydantic, when to choose it over function calling, and the gotchas you’ll encounter in production.

Let's work together

Tell us about your project and how Cuttlesoft can help. Schedule a consultation with one of our experts today.

Contact Us