fast_jsonapi icon indicating copy to clipboard operation
fast_jsonapi copied to clipboard

has_many relationship serialization

Open devtoro opened this issue 7 years ago • 3 comments

Hello everyone!

I recently started using fast_jsonapi in my rails API and I have to say that the performance improvement is amazing!

I just have a suggestion to make. At the moment, in order to serialise "has_many" relationships, the gem is looking for relationship_model_name_ids and queries on that. With active record serializers, it would be enough to have a method to fetch the relationship data, named after the rails convention.

e.g.

Actor has_many :movies could have a method movies and the relationship data would be serialised.

I mention this because I have some models that are Elasticsearch models and I was not storing the relationship ids, instead I had a method to fetch the relationship data ( I was caching the data as well ) and after I switched to fast_jsonapi the relationship was empty! :)

For now, I store the ids in the mode, but I think It's an easy addition for the gem, if it makes sense to you as well.

devtoro avatar Mar 01 '18 14:03 devtoro

@devtoro we always look for relationship_model_name_ids(movie_ids) first as it is an optimization mechanism. It is common to just serialize only the ids unless the relationship is in the includes list. This saves unnecessary fetching of objects and object creation.

Looks like in your case you are saying if the relationship_model_name_ids(movie_ids) method doesn't exist then instead of throwing an error or skipping serialization altogether, we should fallback on the relationship_model_names(s)(movies) method to get the id.

I think this request makes sense. Do you think you can take a shot at implementing it?

shishirmk avatar Mar 18 '18 22:03 shishirmk

PRed! https://github.com/Netflix/fast_jsonapi/pull/151

shuheiktgw avatar Apr 01 '18 13:04 shuheiktgw

Sorry for the late reply...I had certain issues to attend and could not participate further.

What's the status on this?

Is #151 merged? Shall I take some action? I would gladly implement it if it's necessary. :)

devtoro avatar Jul 23 '18 11:07 devtoro