Customize adapter's pathForType to include the related model's id

Suppose, I have a model like the following.

// models/post.js
author: attr('string'),
title: attr('string'),
views: attr('number'),
comments: hasMany('comment')

Now, if i want to reload a particular comment using comment.reload(), my server has the URL in the format


I would like to know what is the best way to construct this url in the comments adapter?

Until now I am taking the currentURL from the router service which will have the post id and then constructing it manually. Is there any other better approach to this?

My first thought would be to override the urlForFindRecord method. In order to get the value for :post_id, you’ll probably need a post: belongsTo('post', { async: false }) relationship on the comment and then use the BelongsToReference API to grab that post ID value, like comment.belongsTo('post').id(). Something like this:

urlForFindRecord(id, modelName, snapshot) {
  let comment = snapshot.record;
  let postId = comment.belongsTo('post').id();
  return `/api/posts/${postId}/comments/${id}`;
1 Like

That is one good solution. Thanks @skaterdav85 :slightly_smiling_face:

1 Like