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