欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

同时分页查找没有关联关系的多个表

程序员文章站 2022-07-15 20:07:53
...
supplyinfos和seekinginfos两个表
需求:  实现供求信息列表,包括supplyinfos和seekinginfos两个表中的所有数据,并且按照created_at倒序排列,而且要求分页

实现:  用了5个小时的时间,终于知道怎么实现了,不多废话了,直接看实现方式,这种方式依赖于will_paginate:

Supplyinfo.paginate_by_sql("select id,name,count,user_id,created_at from supplyinfos where period=0 union all select id,name,count,user_id,created_at from seekinginfos where period = 0 order by created_at desc", :page => params[:page], :per_page => 20)

首先:  注意  unino all  可以把多个表查出来的数据关联到一起,然后paginate_by_sql可以直接使用sql语句,并且有分页功能,就这样,实现了