在Rails应用程序中,我有三个模型(Posting,Shop :: Items和Directory :: Items).每个模型都与其他模型(用户,类别,城市......)有关系.我必须在主页上显示所有这些信息.它应该看起来像分页的项目列表.我正在使用postgres for DB.
我们可以在控制器中编写这样的代码:
@postings = Posting.includes(:category, :subcategory, :user, :city).all
@directory_items = Directory::Item.includes(:category, :user, :city).all
@shop_items = Shop::Item.includes(:category, :user, :city, :phone).all
Run Code Online (Sandbox Code Playgroud)
并对此结果进行排序.但这不是最好的方式.
第二种解决方案是编写自定义查询,省略ActiveRecord.但是这个查询真的很大并且很难支持它(我们有很多连接)
对于数据量最少的数据库查询,最好的解决方案是什么?(这是主页,所以它应该工作得非常快).