Skip to content

Commit f3b2027

Browse files
committed
Added examples to use class_name options for association.
1 parent 41f2e11 commit f3b2027

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

docs/general/serializers.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ e.g.
8181
```ruby
8282
has_one :bio
8383
has_one :blog, key: :site
84+
has_one :blog, class_name: "Blog"
8485
has_one :maker, virtual_value: { id: 1 }
8586

8687
has_one :blog do |serializer|
@@ -114,6 +115,7 @@ e.g.
114115
has_many :comments
115116
has_many :comments, key: :reviews
116117
has_many :comments, serializer: CommentPreviewSerializer
118+
has_many :comments, class_name: "Comment"
117119
has_many :reviews, virtual_value: [{ id: 1 }, { id: 2 }]
118120
has_many :comments, key: :last_comments do
119121
last(1)
@@ -127,6 +129,7 @@ e.g.
127129
```ruby
128130
belongs_to :author, serializer: AuthorPreviewSerializer
129131
belongs_to :author, key: :writer
132+
belongs_to :author, class_name: "Author"
130133
belongs_to :post
131134
belongs_to :blog
132135
def blog
@@ -294,7 +297,7 @@ end
294297
Whether you write the method as above or as `object.comments.where(created_by: scope)`
295298
is a matter of preference (assuming `scope_name` has been set).
296299

297-
Keep in mind that the scope can be set to any available controller reference. This can be utilized to provide access to any other data scopes or presentation helpers.
300+
Keep in mind that the scope can be set to any available controller reference. This can be utilized to provide access to any other data scopes or presentation helpers.
298301

299302
##### Controller Authorization Context
300303

@@ -381,7 +384,7 @@ class PostsController < ActionController::Base
381384
end
382385
end
383386
```
384-
Note that any controller reference which provides the desired scope is acceptable, such as another controller method for loading a different resource or reference to helpers. For example, `ActionController::API` does not include `ActionView::ViewContext`, and would need a different reference for passing any helpers into a serializer via `serialization_scope`.
387+
Note that any controller reference which provides the desired scope is acceptable, such as another controller method for loading a different resource or reference to helpers. For example, `ActionController::API` does not include `ActionView::ViewContext`, and would need a different reference for passing any helpers into a serializer via `serialization_scope`.
385388

386389
#### #read_attribute_for_serialization(key)
387390

0 commit comments

Comments
 (0)