在 Kaminari 中对多个模型进行分页
我正在创建一个搜索页面,将对用户、帖子和评论进行应用程序范围的搜索。我目前有:
# POST /search
def index
query = params[:query]
@users = User.search(query).page(params[:page])
@posts = Post.search(query).page(params[:page])
@comments = Comment.search(query).page(params[:page])
respond_to do |format|
format.html
end
end
但是我真的很想得到一些将所有结果混合在一起然后分页的东西。进行这样的分页搜索有哪些策略?谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
自从这次提交以来: https://github.com/amatsuda/kaminari/commit/f9f529fb68ab89feea38773a4c625c1b14859128
你可以执行以下操作
在你看来你可以这样做:
然后在你的控制器中你可以在这个中引用它们方式:
和你的视图 js.erb 你可能有类似的东西:
Ever since this commit: https://github.com/amatsuda/kaminari/commit/f9f529fb68ab89feea38773a4c625c1b14859128
You can do the following
In your view you can do this:
and then in your controller you can refer to them in this way:
and your view's js.erb you might have something like:
在考虑解决方案之前,您需要首先准确定义您想要的最终结果。如果您想在结果页面上显示每种类型的记录中的一些,您可以修改您发布的方法并使用以下方法组合三个分页结果:
每个对象都需要有一个实例方法“score”,让它根据以下条件进行排序:查询优先级。此外,您还需要修改视图以处理每个项目的正确渲染,并确保在具有最多页面的模型上调用分页。
或者,更可靠的方法是添加全文搜索服务(例如 Index Tank、Web Solr,思考狮身人面像)。这些热门技术的发展速度很快,因此请进行一些研究并找到适合您需求的技术。示例语法如下:
Before thinking about a solution, you need to first define exactly what you want the final result to be. If you want to display a few of each type of record on the results page you can modify the approach you posted and combine the three paginated results using:
Each object will need to have an instance method 'score' that will let it sort based on the query priority. Also, you will need to modify your view to handle correct rendering of each item and ensure that the pagination is called on the model with the most pages.
Alternatively, a more robust method would be to add a full-text search service (such as Index Tank, Web Solr, Thinking Sphinx). The technology for what's hot for these moves quickly, so do some research and find one that fits your needs. Example syntax for this would be something like:
您可以将查询结果与运行页面结合起来。
You could combine the results from the query and run page on that.