Posted By

akmuraro on 06/06/12


Tagged

random


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

akmuraro
jwilcox09


Pick a random element


 / Published in: Rails
 

in your model, add a def to pick a record where an option (in my case lenght) is equal to 1

  1. def self.random
  2. ids = connection.select_all("SELECT id FROM paragraphs WHERE length = 1")
  3. find(ids[rand(ids.length)]["id"].to_i) unless ids.blank?
  4. end

Report this snippet  

You need to login to post a comment.