使用will_paginate在随机的博客文章列表中进行分页 [英] Paginate through a randomized list of blog posts using will_paginate

查看:91
本文介绍了使用will_paginate在随机的博客文章列表中进行分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让用户能够以随机顺序翻阅我的博客文章.

I want to give users the ability to page through my blog posts in random order.

我不能像这样实现它:

@posts = Post.paginate :page => params[:page], :order => 'RANDOM()'

由于每次查询都会调用:order参数,因此我冒着重复博客帖子的风险.

since the :order parameter is called with every query, and therefore I risk repeating blog posts.

做到这一点的最佳方法是什么?

What's the best way to do this?

推荐答案

RAND在MySQL中接受种子:

RAND accepts a seed in MySQL:

RAND(N) 

MySQL文档中:

RAND(),RAND(N)

RAND(), RAND(N)

返回随机浮点值 v在0< = v<范围内1.0.如果一个 常量整数参数N为 指定,用作种子 值,产生可重复的 列值的顺序.在里面 在下面的示例中,请注意RAND(3)生成的值的序列在出现的两个地方都是相同的.

Returns a random floating-point value v in the range 0 <= v < 1.0. If a constant integer argument N is specified, it is used as the seed value, which produces a repeatable sequence of column values. In the following example, note that the sequences of values produced by RAND(3) is the same both places where it occurs.

其他数据库应该具有类似的功能.

Other databases should have similar functionality.

如果每次调用RAND时都使用 SAME 种子,则顺序将在请求中保持一致,因此可以进行分页.

If you use the SAME seed each time you call RAND, the order will be consistent across requests and you can paginate accordingly.

然后,您可以将种子存储在用户的会话中-这样每个用户将看到他们唯一的一组结果.

You can then store the seed in the user's session - so each user will see a set of results unique to them.

这篇关于使用will_paginate在随机的博客文章列表中进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆