-
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
[WIP] Add support for polymorphic associations. #1453
Conversation
I know this seems a |
@@ -78,6 +78,17 @@ def build_association(subject, parent_serializer_options) | |||
|
|||
private | |||
|
|||
def polymorphic_key(assoc_value) | |||
reflection_key = assoc_value.class.model_name | |||
assoc_value.respond_to?(:each) ? reflection_key.plural : reflection_key.singular |
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.
😘 what I need, I'm merging this in a branch of mine 😁
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.
Cool, tell me whether this worked! (usage is has_one :addressable, polymorphic: true
)
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.
works like a charm ;)
|
||
def reflection_options(assoc_value, options) | ||
reflection_options = options.dup | ||
reflection_options[:key] = polymorphic_key(assoc_value) if options[:polymorphic] && !options.key?(:key) |
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.
Note: We might want to make it a ||=
in order not to override user-provided options (instead of testing for !options.key?(:key)
).
After giving it a thought, I'm not sure this feature really makes sense (especially in the case of to-many relationships and empty to-ones). |
Closing, this does not make any sense. |
Properly choose the association key for polymorphic associations.