Posted By

smoothdzion on 10/09/10


Tagged

random


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

smoothdzion


Random Object From an Array


 / Published in: Rails
 

URL: http://www.mycollegesandcareers.com

I created an array of objects, but I wanted to reuse the array and pull one random object out of it so this is the code that I found thanks to this: http://www.ruby-forum.com/topic/144788

  1. <%
  2. # Seems to work for me. I just set the code to a variable.
  3. @array = Model.all(:conditions => "condition_1 = true", :order => "name ASC", :limit => 10)
  4. @random_object = @array[rand(@array.length)]
  5. %>

Report this snippet  

You need to login to post a comment.