简体   繁体   English

Rails Active Record查询来推送两个活动记录关系对象

[英]Rails Active Record Query to push two active record relation objects

first_user = User.first
last_user = User.last

How to add two relation object ? 如何添加两个关系对象? This does not work. 这是行不通的。

my_users = first_user + last_user

I can use array and push the first and last user. 我可以使用数组并推送第一个和最后一个用户。 But pagination will not work for array 但是分页不适用于数组

users = my_users.paginate# (using will_paginate)

But this works 但这有效

users = User.limit(2).paginate

You cannot add two objects,it should be in array format always then only you can do pagination using will paginate. 您不能添加两个对象,它必须始终为数组格式,然后才可以使用分页进行分页。

you have to follow the below step 您必须按照以下步骤

 user_all = [] 

 first_user = User.first 

 last_user = User.last 

 user_all << first_user

 user_all << last_user

 users = user_all.paginate

This will work, but makes at least three SQL calls: 这将起作用,但是至少进行三个SQL调用:

first_user = User.first
last_user = User.last
users = User.where(id: [first_user.id, last_user.id]).paginate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM