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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:20:43+00:00 2026-05-23T09:20:43+00:00

Using Rails 3, and I’m a RoR noob, so this might be simple, but

  • 0

Using Rails 3, and I’m a RoR noob, so this might be simple, but I can’t seem to figure it out. What I mean is, I can get it to work, but I can’t figure out the best way.

Ok, I’ve read every question on SO about selecting random records via Rails, and I think the answer is pretty straight forward, for the most part. However, I’m forced to call an acts_as_taggable_on_steroids method before selecting a random record, so my preferred technique doesn’t work, since find_tagged_with returns an array.

This is a two-part question. I need to know which of these methods are the best, from a performance standpoint, and also how to prevent duplicate posts from being in the results if this method is called more than once with a different tag.name.

Here are the ways I’ve tried so far:

  def related_posts(tag)
    rand_id = rand(Post.find_tagged_with(tag.name).count)
    rand_record = Post.find_tagged_with(tag.name, :conditions => [ "posts.id >= ?", rand_id], :limit => 2)
  end

  def related_posts(tag)
    rand_id = rand(Post.find_tagged_with(tag.name).count)
    post = Post.find_tagged_with(tag.name, :offset => rand_id, :limit => 2)
  end  

  def related_posts(tag)
    post = Post.find_tagged_with(tag.name, :order => 'RAND()', :limit => 2)
  end

  def related_posts(tag)
    posts = Post.find_tagged_with(tag.name)
    offset = rand(posts.count)
    posts.find(:offset =>offset) #doesn't work since this is an array at this point :(
  end

  def related_posts(tag)
    related = []
    posts = Post.find_tagged_with(tag.name)
    related << random_post(posts)
    related << random_post(posts)
    return related
  end
  def random_post(obj)
    rand_id = rand(obj.count)
    rand_record = obj[rand_id]
  end

Edit: This seems to be the fastest, although I have very little experience performance testing rails apps.

  def related_posts(tag)
    posts = Post.find_tagged_with(tag.name).sort_by { rand }.slice(0, 2)
  end

Whoever happens to answer this question, could you also please explain what exactly is happening? Is it randomising the records and sorting on the database level or something else. Also, what does that typically mean performance-wise with a rails app?

  • 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-23T09:20:43+00:00Added an answer on May 23, 2026 at 9:20 am

    I’ll break this line down for ya:

    posts = Post.find_tagged_with(tag.name).sort_by { rand }.slice(0, 2)
    

    The first chained call to find_tagged_with sets up the SQL and performs the query. I imagine it’s an ActiveRecord::Relation object.

    The second, sort_by is a built-in Rails method. So it’s not using the database to do the sorting. This could become pricey if you have tens of thousands of records pulled in the first call. More on this here: http://paulsturgess.co.uk/articles/show/85-ruby-on-rails-sort_by-vs-sort

    Lastly, the slice(0, 2) is an Array method, cutting up your array of results. There’s tons of other ways of doing this, such as .first( 2 ) or [0..2]

    Also, here’s a detailed Ruby on Rails guide to benchmarking (performance testing each method): http://guides.rubyonrails.org/performance_testing.html

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

Sidebar

Related Questions

Using Rails I'm trying to get an error message like The song field can't
using rails with Paperclip, I can use the following to get the filename during
Using Rails 2.3.8. I have added this in my controller: filter_parameter_logging :password, :password_confirmation But
Using Rails 3.1.3 and I'm trying to figure out why our counter caches aren't
Using Rails 3.1.1. & 'rails3-jquery-autocomplete' I think this question should be quite simple for
I'm using rails, but doing all my Javascript unobtrusively using jQuery in application.js .
Using Rails 3.2 and ruby 1.9.3p0 I am trying out the gem delayed_job. I
I am using Rails 3. There is a possible duplicate here . But it
Using: Rails 3.0.3 & Heroku & Execution Notifier & New Relic I get a
I'm using Rails 2.3.2, and trying to get a nested object form to work

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.