fixes #2415: "Random post" causes RecordNotFound exceptions

This commit is contained in:
r888888888
2017-01-18 14:12:12 -08:00
parent a919da4dd6
commit bfa56a860c
4 changed files with 27 additions and 24 deletions

View File

@@ -1579,6 +1579,20 @@ class Post < ActiveRecord::Base
end
module SearchMethods
# returns one single post
def random
key = Digest::MD5.hexdigest(Time.now.to_f.to_s)
random_up(key) || random_down(key)
end
def random_up(key)
where("md5 < ?", key).reorder("md5 desc").first
end
def random_down(key)
where("md5 >= ?", key).reorder("md5 asc").first
end
def pending
where("is_pending = ?", true)
end