-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds a configuration option belongs_to_uses_id_on_self #2140
Adds a configuration option belongs_to_uses_id_on_self #2140
Conversation
- adding documentation and changelog - setting belongs_to_uses_id_on_self to false by default
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Some discussion on improving examples.
@@ -28,6 +28,31 @@ Possible values: | |||
|
|||
When `false`, serializers must be explicitly specified. | |||
|
|||
##### belongs_to_uses_id_on_self | |||
|
|||
Enable automatic serializer lookup when using a `belongs_to` without `has_many`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tells the JSON:API adapter to build
belongs_to
relationship id and type using the foreign key on self, rather than loading the association.
For example, a Comment that belongs to a user will have auser_id
attribute.
This means the relationship will the callobject.user_id
instead ofobject.user.id
, which can prevent unneeded queries.
IMPORTANT: Because the association is no longer be loaded, anytype
must come from thebelongs_to
options astype: :comment_users
orserializer: CommentUserSerializer
(which would call CommentUserSerializer._type, i.e. no instance methods)
or something to that effect. Examples below are good.
|
||
```ruby | ||
class PostSerializer < ActiveModel::Serializer | ||
belongs_to :foo, serializer: FooSerializer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
examples should both be belongs to :blog, but one uses the type
option and one uses the serializer
option, and then show how they differ in their serialized relationship given a Post with blog_id = 1 and different BlogSerializer configurations, and what happens when both are given, or if the relationship is included...
end | ||
class BelongsToBlogWithCustomTypeModelSerializer < ActiveModel::Serializer | ||
type :posts | ||
belongs_to :blog_with_custom_type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also good to include an instance method json_key
to show it's not used
belongs_to :blog_with_custom_type | ||
end | ||
|
||
def test_belongs_to_allows_type_overwriting |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test_belongs_to_doesnt_load_record
should also change, no, since it requires belongs_to_uses_id_on_self
to be true, no?
@@ -43,7 +43,8 @@ def data_for(association) | |||
end | |||
|
|||
def data_for_one(association) | |||
if association.belongs_to? && | |||
if !ActiveModelSerializers.config.belongs_to_uses_id_on_self && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be
- if !ActiveModelSerializers.config.belongs_to_uses_id_on_self &&
+ if ActiveModelSerializers.config.belongs_to_uses_id_on_self &&
@bf4 Just to clarify, do we want |
Purpose
Adds a configuration option
belongs_to_uses_id_on_self
to allow for automatic serializer lookup. This solves an issue where the customtype
attribute was not being used as the type was inferred from the associated object.Changes
ActiveModelSerializers.config.belongs_to_uses_id_on_self
Caveats
belongs_to_uses_id_on_self
to true will result in a lookup on the associated object,Related GitHub issues
Closes #2125
Additional helpful information