File tree Expand file tree Collapse file tree 2 files changed +6
-1
lines changed
lib/active_model/serializer Expand file tree Collapse file tree 2 files changed +6
-1
lines changed Original file line number Diff line number Diff line change 64
64
- ` unless: `
65
65
- ` virtual_value: `
66
66
- ` polymorphic: ` defines if polymorphic relation type should be nested in serialized association.
67
+ - ` type: ` the resource type as used by JSON: API , especially on a ` belongs_to ` relationship.
68
+ - ` class_name: ` used to determine ` type ` when ` type ` not given
69
+ - ` foreign_key: ` used by JSON: API on a ` belongs_to ` relationship to avoid unnecessarily loading the association object.
67
70
- optional: ` &block ` is a context that returns the association's attributes.
68
71
- prevents ` association_name ` method from being called.
69
72
- return value of block is used as the association value.
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ class Serializer
9
9
# @example
10
10
# class PostSerializer < ActiveModel::Serializer
11
11
# has_one :author, serializer: AuthorSerializer
12
+ # belongs_to :boss, type: :users, foreign_key: :boss_id
12
13
# has_many :comments
13
14
# has_many :comments, key: :last_comments do
14
15
# object.comments.last(1)
@@ -58,12 +59,13 @@ def initialize(*)
58
59
class_name = options . fetch ( :class_name , name . to_s . camelize . singularize )
59
60
class_name . underscore . pluralize . to_sym
60
61
end
61
- @foreign_key =
62
+ @foreign_key = options . fetch ( :foreign_key ) do
62
63
if collection?
63
64
"#{ name . to_s . singularize } _ids" . to_sym
64
65
else
65
66
"#{ name } _id" . to_sym
66
67
end
68
+ end
67
69
end
68
70
69
71
# @api public
You can’t perform that action at this time.
0 commit comments