Ruby on Rails - Relation pagination with active record and Kaminari

Asked

Viewed 165 times

1

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

I’m having a hard time paging the results of my following list, I also wanted to display only follow and not the user data as is in the return below:

Note: I am using the Kaminari yolk for paging

Method of my controller

def followeres
    @user = User.find_by(username: params[:username])

    render json: @user, include: [:followers]
end

Model User

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

[...]

Current return

{
    "user": {
        "username": "teste123",
        "full_name": "Teste 123",
        "profile_pic_url": null,
        "genre": null,
        "phone": "41922222222",
        "email": "[email protected]",
        "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
            },
            [...]
        ]
    }
}

My attempt, but not page

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

1 answer

0

Updating...

I changed my controller to the code below and is now paging...

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 it is not returning the user data as username, name etc. that is configured in the serializer.

Answer:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.