Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7632751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:42:19+00:00 2026-05-31T06:42:19+00:00

I have an app where I retrieve a list of users from a specific

  • 0

I have an app where I retrieve a list of users from a specific country.

I did this in the UsersController:

@fromcanada = User.find(:all, :conditions => { :country => 'canada' })

and then turned it into a scope on the User model

scope :canada, where(:country => 'Canada').order('created_at DESC')

but I also want to be able to retrieve a random person or multiple persons from the country. I found this method that’s supposed to be an efficient way to retrieve a random user from the database.

module ActiveRecord
  class Base
    def self.random
      if (c = count) != 0
        find(:first, :offset =>rand(c))
      end
    end
  end
end

However, I have a few questions about how to add it, and how the syntax works.

  1. Where would I put that code? Direct in the User model?

  2. Syntax: so that I don’t use code that I don’t understand, can you explain how the syntax is working? I don’t get (c = count). What is count counting? What is rand(c) doing? Is it finding the first one starting at the offset? If rand is an expensive method (hence the need to create a different more efficient random method), why use the expensive ‘rand‘ in this new more efficient random method?

  3. How could I add the call to random on my find method in the UsersController? How to add it to the scope in the model?

  4. Building on question 3, is there a way to get two or three random users?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:42:20+00:00Added an answer on May 31, 2026 at 6:42 am

    I wouldn’t monkey patch that (or anything else!) into ActiveRecord, putting that into your User would make more sense.

    The count is counting how many elements there are in your table and storing that number in c. Then rand(c) gives you a random integer in the interval [0,c) (i.e. 0 <= rand(c) < c). The :offset works the way you think it does.

    rand isn’t terribly expensive but doing order by random() inside the database can be very expensive. The random method that you’re looking at is just a convenient way to get a random record/object from the database.

    Adding it to your own User would look something like this:

    def self.random
      n = scoped.count
      scoped.offset(rand(n)).first
    end
    

    That would allow you to chain random after a bunch of scopes:

    u = User.canadians_eh.some_other_scope.random
    

    but the result of random would be a single user so your chaining would stop there.

    If you wanted multiple users you’d want to call random multiple times until you got the number of users you wanted. You could try this:

    def self.random
      n = scoped.count
      scoped.offset(rand(n))
    end
    
    us = User.canadians_eh.random.limit(3)
    

    to get three random users but the users would be clustered together in whatever order the database ended up with after your other scopes and that’s probably not what you’re after. If you want three you’d be better off with something like this:

    # In User...
    def self.random
      n = scoped.count
      scoped.offset(rand(n)).first
    end
    
    # Somewhere else...
    scopes = User.canadians_eh.some_other_scope
    users  = 3.times.each_with_object([]) do |_, users|
      users << scopes.random
      scopes = scopes.where('id != :latest', :latest => users.last.id)
    end
    

    You’d just grab a random user, update your scope chain to exclude them, and repeat until you’re done. You would, of course, want to make sure you had three users first.

    You might want to move the ordering out of your canada scope: one scope, one task.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an app that allows the user to select from a list
I have an app in android which retrieve a picture from http page.I'm asking
I have made app which allows me to send message to multiple user from
I have an app that attempts to retrieve an iAd and if is unsuccessful
We have App A as main app. Now we build from it App B
I have an app that needs to read a PDF file from the file
I would like to have a textbox in a Silverlight app where the user
I have a tableView storing a list of notifications (retrieved from server). How can
I have an app where I would like to select a user in my
Greetings! I have an ASP.NET app that scrapes data from a handful of external

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.