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 6464 - ` unless: `
6565 - ` virtual_value: `
6666 - ` 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.
6770- optional: ` &block ` is a context that returns the association's attributes.
6871 - prevents ` association_name ` method from being called.
6972 - 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
99 # @example
1010 # class PostSerializer < ActiveModel::Serializer
1111 # has_one :author, serializer: AuthorSerializer
12+ # belongs_to :boss, type: :users, foreign_key: :boss_id
1213 # has_many :comments
1314 # has_many :comments, key: :last_comments do
1415 # object.comments.last(1)
@@ -58,12 +59,13 @@ def initialize(*)
5859 class_name = options . fetch ( :class_name , name . to_s . camelize . singularize )
5960 class_name . underscore . pluralize . to_sym
6061 end
61- @foreign_key =
62+ @foreign_key = options . fetch ( :foreign_key ) do
6263 if collection?
6364 "#{ name . to_s . singularize } _ids" . to_sym
6465 else
6566 "#{ name } _id" . to_sym
6667 end
68+ end
6769 end
6870
6971 # @api public
You can’t perform that action at this time.
0 commit comments