Improvements for flat_one #27
aaronrosenthal
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
First let me say I love the idea of
flat_one
. But I think it has an issue that I'd like to see improved.It's pretty common that an association can be null in the schema, and simply using
flat_one
will result in errors when trying to serialize the attributes of the association. Since we have conditionality it's easily resolved with:but from my experience so far there's never a case where I wouldn't add conditionality. I think there's a few improvements that could fix this:
Solution 1
Make the conditionality implicit for
flat
associations. By this I mean the library should not even attempt to serialize the values fornil
associations, similar to howone_if
works. This would eliminate the need to ever makeflat_one
explicitly conditional.Solution 2
Similar to above, but instead of implicit conditionality just serialize
null
values for all the attributes inflat_one
's serializer. One advantage of this solution is it allows flexibility in whether the entire serialization should be conditional or if it should returnnull
values in the JSON.Solution 3
Allow passing a block to
flat_one
for additional flexibility. For example:as one way to workaround nil references. This could also be useful for serializing stored JSON in the schema (which could be nullable) with serializers that use
hash_attributes
. Contrived example below:Beta Was this translation helpful? Give feedback.
All reactions