作者:唉尼宝宝_586 | 来源:互联网 | 2023-09-04 17:31
After documenting #64, I felt a little odd about the
option I added to the
|
|
methods:
1 2 3 4 5
| ruby
class ProjectSerializer
include FastJsonapi::ObjectSerializer
belongs_to :owner, polymorphic: { User => :user, Administrator => :administrator }
end |
I think the
option describes an ORM feature (specially when working with relational databases) rather than an object serialization aspect.
Actually, I think the jsonapi spec doesn't even care if an association is polymorphic or not, as long as the associated object has the
key in it, it's all good. I think this should be reflected in the serializer DSL.
So, how about replacing the
option with being able to use the
option with either a symbol (as it is right now) or the
object
receives now?
1 2 3 4 5
| ruby
class ProjectSerializer
include FastJsonapi::ObjectSerializer
belongs_to :owner, record_type: { User => :user, Administrator => :administrator }
end |
As a matter of fact, since we're memoizing the associated object => record_type with #64 when the
isn't provided (Thanks for the tip), I think we can make pretty much the same (performance wise) to not need to specify a
at all:
1 2 3 4 5
| ruby
class ProjectSerializer
include FastJsonapi::ObjectSerializer
belongs_to :owner # The auto-detect fallback + memoization added in #64 would kick in
end |
(You could still define a
for
, but that would give just a very marginal performance gain when serializing 1 object, I think)
Your thoughts ?
该提问来源于开源项目:Netflix/fast_jsonapi
Would you mind commenting on the current issue describing why the approach won't work for your situation? We should be able to help one way or another. Thanks!