You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/general/getting_started.md
+31Lines changed: 31 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -57,6 +57,37 @@ class CommentSerializer < ActiveModel::Serializer
57
57
end
58
58
```
59
59
60
+
### Namespaced Models
61
+
62
+
When serializing a model inside a namespace, such as `Api::V1::Post`, AMS will expect the corresponding serializer to be inside the same namespace (namely `Api::V1::PostSerializer`).
63
+
64
+
### Model Associations and Nested Serializers
65
+
66
+
When declaring a serializer for a model with associations, such as:
67
+
```ruby
68
+
classPostSerializer < ActiveModel::Serializer
69
+
has_many :comments
70
+
end
71
+
```
72
+
AMS will look for `PostSerializer::CommentSerializer` in priority, and fall back to `::CommentSerializer` in case the former does not exist. This allows for more control over the way a model gets serialized as an association of an other model.
73
+
74
+
For example, in the following situation:
75
+
76
+
```ruby
77
+
classCommentSerializer < ActiveModel::Serializer
78
+
attributes :body, :date, :nb_likes
79
+
end
80
+
81
+
classPostSerializer < ActiveModel::Serializer
82
+
has_many :comments
83
+
classCommentSerializer < ActiveModel::Serializer
84
+
attributes :body_short
85
+
end
86
+
end
87
+
```
88
+
89
+
AMS will use `PostSerializer::CommentSerializer` (thus including only the `:body_short` attribute) when serializing a `Comment` as part of a `Post`, but use `::CommentSerializer` when serializing a `Comment` directly (thus including `:body, :date, :nb_likes`).
90
+
60
91
## Rails Integration
61
92
62
93
AMS will automatically integrate with you Rails app, you won't need to update your controller, this is a example of how it will look like:
0 commit comments