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: README.md
+9-1Lines changed: 9 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -149,12 +149,20 @@ Note that the JSON:API spec distinguishes in how null/empty is handled for singl
149
149
150
150
### Custom attributes
151
151
152
-
By default the serializer looks for the same name of the attribute on the object it is given. You can customize this behavior by providing a block to the attribute:
152
+
By default the serializer looks for the same name of the attribute on the object it is given. You can customize this behavior by providing a block to `attribute`, `has_one`, or `has_many`:
153
153
154
154
```ruby
155
155
attribute :contentdo
156
156
object.body
157
157
end
158
+
159
+
has_one :commentdo
160
+
Comment.where(post: object).take!
161
+
end
162
+
163
+
has_many :authorsdo
164
+
Author.where(post: object)
165
+
end
158
166
```
159
167
160
168
The block is evaluated within the serializer instance, so it has access to the `object` and `context` instance variables.
0 commit comments