繁体   English   中英

如何在rails上分享与ruby的关系?

[英]How to paginate relations with ruby ​on rails?

我正在学习Ruby On Rails,作为一个学习目标,我使用Instagram的基本功能制作了一个API。

我很难对我的关注者列表的结果进行分页。

我还想只显示关注者而不显示用户数据,因为它在下面没有返回:

PS。 我正在使用kaminari宝石进行分页

调节器

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

返回

{
    "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上获取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

暂无
暂无

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

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