File tree Expand file tree Collapse file tree 2 files changed +11
-3
lines changed
lib/active_model/serializer Expand file tree Collapse file tree 2 files changed +11
-3
lines changed Original file line number Diff line number Diff line change @@ -7,8 +7,16 @@ class Serializer
7
7
# class PostSerializer < ActiveModel::Serializer
8
8
# has_one :author, serializer: AuthorSerializer
9
9
# has_many :comments
10
+ # has_many :comments, key: :last_comments do
11
+ # last(1)
12
+ # end
10
13
# end
11
14
#
15
+ # Notice that the association block is evaluated in the context of the association.
16
+ # Specifically, the association 'comments' is evaluated two different ways:
17
+ # 1) as 'comments' and named 'comments'.
18
+ # 2) as 'comments.last(1)' and named 'last_comments'.
19
+ #
12
20
# PostSerializer._reflections #=>
13
21
# # [
14
22
# # HasOneReflection.new(:author, serializer: AuthorSerializer),
@@ -33,7 +41,7 @@ def value(instance)
33
41
34
42
def self . build_reader ( name , block )
35
43
if block
36
- -> ( instance ) { instance . instance_eval ( &block ) }
44
+ -> ( instance ) { instance . read_attribute_for_serialization ( name ) . instance_eval ( &block ) }
37
45
else
38
46
-> ( instance ) { instance . read_attribute_for_serialization ( name ) }
39
47
end
Original file line number Diff line number Diff line change @@ -128,8 +128,8 @@ def test_associations_custom_keys
128
128
129
129
class InlineAssociationTestPostSerializer < ActiveModel ::Serializer
130
130
has_many :comments
131
- has_many :last_comments do
132
- object . comments . last ( 1 )
131
+ has_many :comments , key : : last_comments do
132
+ last ( 1 )
133
133
end
134
134
end
135
135
You can’t perform that action at this time.
0 commit comments