Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions docs/howto/add_relationship_links.md
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ class Api::V1::UserSerializer < ActiveModel::Serializer
end
```

This will resilt in (example is in jsonapi adapter):
This will result in (example is in jsonapi adapter):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

```json
{
"data": {
@@ -69,7 +69,7 @@ class Api::V1::UserSerializer < ActiveModel::Serializer
end
```

This will resilt in (example is in jsonapi adapter):
This will result in (example is in jsonapi adapter):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

```json
{
"data": {
@@ -104,12 +104,7 @@ class Api::V1::UserSerializer < ActiveModel::Serializer

has_many :microposts, serializer: Api::V1::MicropostSerializer do
link(:related) { api_v1_microposts_path(user_id: object.id) }
end

#this is needed to avoid n+1, gem core devs are working to remove this necessity
#more on: https://github.com/rails-api/active_model_serializers/issues/1325
def microposts
object.microposts.loaded ? object.microposts : object.microposts.none
include_data false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ksamc I think this comment is still desired cc @richmolj ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this specific comment, I think it is no longer needed. include_data false is already documented elsewhere as avoiding the N+1/loading the data.

That said, we should probably rework this section. It's totally valid to have both data and links, so there's really no inherent reason to have include_data false in the documentation example in the first place. We're just talking about link generation here.

Instead, I'd add a section below this (or elsewhere) on conditionally including data, how and why one would do this. @ksamc any interest in adding this documentation, based on the explanation in #1931? Otherwise I will submit a separate PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@richmolj I would say the opposite is more often: if you add links you probably don't include the relationship.

I would suggest to merge this request and you can start a new one about conditionally adding data (probably in another section of the wiki)

end
end
```
@@ -126,7 +121,6 @@ This will result in:
},
"relationships": {
"microposts": {
"data": [],
"links": {
"related": "/api/v1/microposts?user_id=1"
}