aglushkov / serega

MIT License
7 stars 0 forks source link

Add current plan point as third parameter to attribute value #152

Open aglushkov opened 8 months ago

aglushkov commented 8 months ago

It can help to omit N+1 for cases, when requested inner rescord which is not a activerecord a association, but it can have other associations to preload

Example:

class API::CurrentUserSerializer < ApplicationSerializer
  attribute :current_order, serializer: API::OrderSerializer, value: -> { |user,  _ctx, point|
     order = user.orders.draft.first
     associations = point.preloads
     ActiveRecord::Associations::Preloader.new(records: [order], associations: associations, available_records: [user]).()      if associations.present?
    ).()
    order
  }
end
aglushkov commented 7 months ago

We can't omit N+1 in this case as all value blocks are serialized one by one and we will preload associations to each such value.

But having point link is not a bad idea, it can be still helpful for some scenarios