Search code examples
ruby-on-railsactive-model-serializersjson-api

Scoping an associations on JSONAPI endpoint in Rails serializer


Is there a way to scope this endpoint (User#Show) on my Rails API, maybe in the serializer? I want to return only the user's last delivery_address, not all of them. I don't want it to effect non API requests.

def show @user = User.find(params[:id]) respond_with @user, include: ['delivery_addresses'] end

I have tried things like 'delivery_address.last', but that doesn't work and I can't find an explanation on the AMS repo...


Solution

  • In your serializer specify attribute :last_delivery and add the method def last_delivery; object.delivery_addresses.last; end if I understand your question