Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: each in random order

This is my code:

    <% question.answers.each do |answer| %>


      <li><%= answer.content %></li>


    <% end %>

I want to order the answer objects randomly. What would be the most efficient way to do so considering the amount of possible answers is less than say 10?

like image 655
Tom Avatar asked Oct 29 '12 14:10

Tom


2 Answers

You can use the shuffle method like this:

question.answers.shuffle.each do |answer|
like image 129
Mischa Avatar answered Oct 21 '22 09:10

Mischa


How about this

<% question.answers.shuffle.each do |answer| %>


      <li><%= answer.content %></li>


    <% end %>
like image 3
Viren Avatar answered Oct 21 '22 11:10

Viren