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 9023167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:41:49+00:00 2026-06-16T05:41:49+00:00

I need to select random records from db. In Sqlite3, which I use on

  • 0

I need to select random records from db. In Sqlite3, which I use on development, there is a function called Random(). However, in Postgresql it’s called Rand(). I don’t remember about MySql, but probably it’s called so there.

So if I have a code of (for Sqlite3)

data = Items.where(pubshied: is_pubshied).order("RANDOM()").limit(count)

how do I ensure that it will work with different databases?

  • 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-06-16T05:41:50+00:00Added an answer on June 16, 2026 at 5:41 am

    Rails doesn’t support this out of the box. I believe I achieved this with a model extension (I dont use it anymore because I force the use of Postgresql), but something like this could work:

    module Randomize
      extend ActiveSupport::Concern
    
      included do
        scope :random, -> { order(rand_cmd) }
      end
    
      module ClassMethods
        def rand_cmd
          if connection.adapter_name =~ /mysql/i
            'rand()'
          else
            'random()'
          end
        end
      end
    end
    

    You can then do

    class Item
      include Randomize
    end
    
    Item.where(...).random.limit(...)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hallo and good sunday to everybody. I need to select N random records from
I need to select only 20 records from the table and than add pagination
I need to get 5 random records from a table plus a further record
I have this pattern where I need to select any random element from a
I need a quick algorithm to select 5 random elements from a generic list.
Possible Duplicate: MySQL select 10 random rows from 600K rows fast There are many
I want to get 1000 records from a table randomly, so I use: SELECT
Possible Duplicate: MySQL select 10 random rows from 600K rows fast I need to
SCENARIO I need to select records from test_userData based on a 1-to-1 match from
I need to select a random item from a table where a value occurs

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.