How to implement nested resources using jsonapi-resorces gem in rails?

I have a Posts resource and a User Rating resource.

In mine, routes.rbI put a user rating in posts:

jsonapi_resources :posts do
  jsonapi_resources :user_ratings
end

This correctly showed the end point /posts/{id}/user-ratings.

However, when testing this endpoint with, {id}it returns everything user-ratings, not just those user-ratingsassociated with this message.

In my resource I have tried to add a message: has_many :user_ratingsI tried to add to user ratings has_one :post.

In my models, I added has_manyand belongs_to.

However, I am not trying to do anything to make the api return only user ratings associated with a specific post id.

Any insight would be appreciated, thanks.

+4
3

, route.rb:

jsonapi_resources :posts do
  jsonapi_related_resources :user_ratings
end

has_many :user_ratings

//{id}/user_ratings, , records_for_ *, :

def records_for_user_ratings
   super.published
end
+2

UserRating a has_one :issue

routes.rb user_ratings posts

rake routes, posts\:post_id\user_ratings, route.rb

+1

, post_id user_ratings. :

    # GET /user_ratings
    def index
      @post = Post.find(params[:post_id])
      @user_ratings = @post.user_ratings

      render json: @user_ratings
    end

, .

-3

All Articles