danabr75 / rails_json_serializer

A Lightweight Rails JSON Serializer (gem library source-code)
GNU Lesser General Public License v3.0
0 stars 0 forks source link

Issue with eager-loading polymorphic associations #15

Open danabr75 opened 3 years ago

danabr75 commented 3 years ago

Looks to be an issue with eager-loading polymorphic associations on an ActiveRecordRelation. Attempting to eager-load them on an individual activerecord object works fine (not that it CAN eager-load them, it just doesn't return an empty association list). I'll be looking into this when I get some free development time, but in the meantime, the work-around is to disable-eager-loading on polymorphic associations. ex:

module PolymorphicClassHereSerializer
  def serializer_query opts = {}
    {
      :include => <...>,
      methods: <...>,
      cache_key: nil,
      skip_eager_loading: true,
    }
  end
end

module UserSerializer
  def serializer_query opts = {}
    {
      :include => {
        polymorphic_class_heres: PolymorphicClassHereSerializer.serializer_query,
      },
      methods: <...>,
      cache_key: __callee__,
    }
  end
end
danabr75 commented 3 years ago

Reported from: rails (5.2.6) ruby 2.7.2p137