module Decidim::Randomable

A concern with the components needed when you want a model to have a component.

Public Instance Methods

order_randomly(seed) click to toggle source

Public: Randomly orders a collection given a seed.

# File lib/decidim/randomable.rb, line 12
def order_randomly(seed)
  transaction do
    connection.execute("SELECT setseed(#{connection.quote(seed)})")
    # Include the record IDs as a base number for the order calculation
    # in order to avoid PostgreSQL random ordering when the records are
    # updated. PostgreSQL can randomly change the base ordering in case
    # the records are changed which is not desired as we want consistent
    # orders for the records.
    order(arel_table[primary_key] * Arel.sql("RANDOM()")).load
  end
end