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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:58:01+00:00 2026-06-15T08:58:01+00:00

I am trying to pull out rows from a postgres database, I can pull

  • 0

I am trying to pull out rows from a postgres database, I can pull them out desc but when I try random I get a Syntax error near random.

Error

PG::Error: ERROR:  syntax error at or near "rand"
LINE 1: ...  "hashtags".* FROM "hashtags"  ORDER BY tweet_id rand LIMIT...
                                                             ^
: SELECT  "hashtags".* FROM "hashtags"  ORDER BY tweet_id rand LIMIT 4

Code to pull it out

<div id="hashtags">
<% Hashtag.order("tweet_id desc").limit(4).each do |hashtag| %>
   <blockquote><%= hashtag.content %></blockquote>
   <div class="from">&mdash; @<%= hashtag.screen_name %></div>
 <% end %>
</div>
  • 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-15T08:58:03+00:00Added an answer on June 15, 2026 at 8:58 am

    To fetch random entries from your database you have a few options. Here’s a couple

    1st approach

    This will take 4 random entries out of your DB using SQL.

    Hashtag.order("RANDOM()").limit(4)
    

    2nd approach:

    You can also use ActiveRecord sample() method to retrieve 4 random rows.

    Hashtag.all.sample(4)
    

    As of speed and efficiency; I made a mini-benchmark and tested two commands on my own db (contains 500 records).
    The first approach (as expected) was more than twice faster than the second approach.

    SQL: 1.8ms
    Sample Method: 4.2ms
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to pull out a list of table names from a MySQL database.
I'm trying to pull out information from a database with PHP and have the
Trying to pull rows from a table and list them in an ol, however,
I'm trying to pull out the sales totals for quarter 1. I can't seem
I am trying to pull out data from the table I had from the
im having some difficulty trying to pull out a specific value from a cookie.
I'm trying to pull out page source from a set of pages and run
I am trying to pull out a number from a string like so item_2445_205
So I am trying to pull out videos from Youtube for specific video games.
I am trying to pull out some HTML (a hrefs) from a long string

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.