如何在 rails 上对与 ruby 的关系进行分页?
How to paginate relations with ruby on rails?
我正在 Ruby Rails 上学习,作为学习目标,我制作了一个具有 Instagram 基本功能的 API。
我很难对关注者列表的结果进行分页。
我还想只显示关注者而不是用户数据,因为下面没有 return:
ps。我正在使用 kaminari gem 进行分页
控制器
def followeres
@user = User.find_by(username: params[:username])
@user.follower_relationships
.page(params[:page] || 1)
.per(1)
.order('created_at DESC')
render json: @user, include: [:followers]
end
型号user.rb
has_many :follows
has_many :follower_relationships, foreign_key: :following_id, class_name: 'Follow', dependent: :destroy
has_many :followers, through: :follower_relationships, source: :follower
has_many :following_relationships, foreign_key: :user_id, class_name: 'Follow', dependent: :destroy
has_many :following, through: :following_relationships, source: :following
[...]
user_serializer.rb
class UserSerializer < ActiveModel::Serializer
include Rails.application.routes.url_helpers
attributes :username, :full_name, :profile_pic_url, :genre, :phone, :email, :bio, :website, :is_private, :is_verified, :followers_count, :following_count
has_many :followers, serializer: FollowSerializer
has_many :following, serializer: FollowSerializer
def profile_pic_url
rails_blob_path(object.image) if object.image.attachment
end
end
follow_serializer.rb
class FollowSerializer < ActiveModel::Serializer
attributes :username, :full_name, :followers_count, :following_count, :is_private
end
Return
{
"user": {
"username": "teste123",
"full_name": "Teste 123",
"profile_pic_url": null,
"genre": null,
"phone": "41922222222",
"email": "example@domain.com",
"bio": null,
"website": null,
"is_private": 1,
"is_verified": 0,
"followers_count": 48,
"following_count": 31,
"followers": [
{
"username": "milagro_ullrich",
"full_name": "Lance Bartell",
"followers_count": 42,
"following_count": 37,
"is_private": 0
},
{
"username": "geoffrey_howell",
"full_name": "Jordon Ritchie",
"followers_count": 34,
"following_count": 38,
"is_private": 0
},
[...]
]
}
}
我试过了
def followeres
@user = User.find_by(username: params[:username])
@followers = @user.follower_relationships
.page(params[:page] || 1)
.per(params[:per_page] || 20)
.order('created_at DESC')
render json: { followers: @followers }, status: :ok
end
但响应
{
"followers": [
{
"id": 3020,
"user_id": 293,
"following_id": 2,
"created_at": "2019-07-25T19:22:37.000Z"
},
{
"id": 2983,
"user_id": 63,
"following_id": 2,
"created_at": "2019-07-25T19:22:37.000Z"
},
[....]
]
}
您应该在 @user
上获取 followers
而不是 follower_relationships
:
def followeres
@user = User.find_by(username: params[:username])
@followers = @user.followers
.page(params[:page] || 1)
.per(params[:per_page] || 20)
.order('created_at DESC')
render json: { followers: @followers }, status: :ok
end
我正在 Ruby Rails 上学习,作为学习目标,我制作了一个具有 Instagram 基本功能的 API。
我很难对关注者列表的结果进行分页。
我还想只显示关注者而不是用户数据,因为下面没有 return:
ps。我正在使用 kaminari gem 进行分页
控制器
def followeres
@user = User.find_by(username: params[:username])
@user.follower_relationships
.page(params[:page] || 1)
.per(1)
.order('created_at DESC')
render json: @user, include: [:followers]
end
型号user.rb
has_many :follows
has_many :follower_relationships, foreign_key: :following_id, class_name: 'Follow', dependent: :destroy
has_many :followers, through: :follower_relationships, source: :follower
has_many :following_relationships, foreign_key: :user_id, class_name: 'Follow', dependent: :destroy
has_many :following, through: :following_relationships, source: :following
[...]
user_serializer.rb
class UserSerializer < ActiveModel::Serializer
include Rails.application.routes.url_helpers
attributes :username, :full_name, :profile_pic_url, :genre, :phone, :email, :bio, :website, :is_private, :is_verified, :followers_count, :following_count
has_many :followers, serializer: FollowSerializer
has_many :following, serializer: FollowSerializer
def profile_pic_url
rails_blob_path(object.image) if object.image.attachment
end
end
follow_serializer.rb
class FollowSerializer < ActiveModel::Serializer
attributes :username, :full_name, :followers_count, :following_count, :is_private
end
Return
{
"user": {
"username": "teste123",
"full_name": "Teste 123",
"profile_pic_url": null,
"genre": null,
"phone": "41922222222",
"email": "example@domain.com",
"bio": null,
"website": null,
"is_private": 1,
"is_verified": 0,
"followers_count": 48,
"following_count": 31,
"followers": [
{
"username": "milagro_ullrich",
"full_name": "Lance Bartell",
"followers_count": 42,
"following_count": 37,
"is_private": 0
},
{
"username": "geoffrey_howell",
"full_name": "Jordon Ritchie",
"followers_count": 34,
"following_count": 38,
"is_private": 0
},
[...]
]
}
}
我试过了
def followeres
@user = User.find_by(username: params[:username])
@followers = @user.follower_relationships
.page(params[:page] || 1)
.per(params[:per_page] || 20)
.order('created_at DESC')
render json: { followers: @followers }, status: :ok
end
但响应
{
"followers": [
{
"id": 3020,
"user_id": 293,
"following_id": 2,
"created_at": "2019-07-25T19:22:37.000Z"
},
{
"id": 2983,
"user_id": 63,
"following_id": 2,
"created_at": "2019-07-25T19:22:37.000Z"
},
[....]
]
}
您应该在 @user
上获取 followers
而不是 follower_relationships
:
def followeres
@user = User.find_by(username: params[:username])
@followers = @user.followers
.page(params[:page] || 1)
.per(params[:per_page] || 20)
.order('created_at DESC')
render json: { followers: @followers }, status: :ok
end