Pick a random element


/ Published in: Rails
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.