lib/randumb/relation.rb
# Returns all matching ids from the db, shuffles them,
# then returns an array containing at most max_ids
Returns all matching ids from the db, shuffles them,
then returns an array containing at most max_ids
Returns all matching ids from the db shuffles them then returns an array containing at most max_ids
def fetch_random_ids(relation, max_ids, opts = {})
        relation.select_values = []
        relation.includes_values = []
        id_only_relation = relation.select("#{table_name}.id")
        id_results = connection.select_all(id_only_relation.to_sql)
        rng = random_number_generator(opts)
        if max_ids == 1 && id_results.count > 0
          rand_index = rng.rand(id_results.count)
          [id_results[rand_index]["id"]]
        else
          arr = id_results.respond_to?(:to_a) ? id_results.to_a : id_results
          arr.shuffle!(random: rng)[0, max_ids].collect! { |h| h["id"] }
        end
      end
def fetch_random_ids ( relation , max_ids , opts = { } ) relation . select_values = [ ] relation . includes_values = [ ] id_only_relation = relation . select ( "#{table_name}.id" ) id_results = connection . select_all ( id_only_relation . to_sql ) rng = random_number_generator ( opts ) if max_ids == 1 && id_results . count > 0 rand_index = rng . rand ( id_results . count ) [ id_results [ rand_index ] [ "id" ] ] else arr = id_results . respond_to? ( :to_a ) ? id_results . to_a : id_results arr . shuffle! ( random : rng ) [ 0 , max_ids ] . collect! { | h | h [ "id" ] } end end
Returns all matching ids from the db, shuffles them,
then returns an array containing at most max_ids
Returns all matching ids from the db shuffles them then returns an array containing at most max_ids
# clear these for our id only query # do original query but only for id field # ActiveRecord 4 requires .to_a
relation
max_ids
opts