ruby-on-railsrubyrails-activerecordkaminari

How to paginate relations with ruby ​on rails?


I'm studying Ruby On Rails and as a learning goal I made an API with the basic functionality of Instagram.

I have difficulty paginating the results of my follower list.

I also wanted to display only followers and not user data as it has no return below:

ps. I am using the kaminari gem for pagination

Controller

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

Model 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
            },
            [...]
        ]
    }
}

I tried

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

but the response

{
    "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"
        },
        [....]
    ]
}

Solution

  • You should fetch followers on @user instead of 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