jeudi 2 juin 2016

How to make ember, rails and active_model_serializers execute one query on hasMany relationship?

I am using ember with rails and active_model_serializers.

Lets say I have a model called post and it has many comments. Lets say one post has 2000 comments. When I call:

post.get('comments').then(comments => { //Do something});

I get 2000 separate api calls to comments and 2000 separate selects from my db.

When I add include:['comments'] to the controller method like so:

def show
  render json: @post, include:['comments']
end

I get the same issue.

I am not sure if there is something I have to do the PostSerializer.rb file to enable to one query as oppose to 2000 for the comments relationship.

In a rails only application a call to my association generates one query. And while I realize ember is not rails, I figure I am missing something simple.

So, how do I make accessing the association on my model generate one api call and one query? Or at least minimize the amount of api calls and queries?




Aucun commentaire:

Enregistrer un commentaire