active_model_serializers
active_model_serializers copied to clipboard
v0.8.4 does not properly include some associations
Expected behavior vs actual behavior
In our serializer we have the following code (mildly streamlined for clarity). The last_sale
relation does not exist on the main model but is defined via a method in the serializer:
class SalesSerializer < ActiveModel::Serializer
attributes :total, :financed, :cash, :gross, :qtd
has_one :last_sale, serializer: LastSoldListingSerializer
def last_sale
Listing.with_status(:sold).order(:closed_at).last
end
end
When running with the gem version 0.8.3, this works properly – the method is called, and the key last_sale
is included in the output of the serializer.
When running with the gem version 0.8.4, this does not work – the key last_sale
is NOT included in the output, and the method last_sale
is never called.
Presumably this change to existing behavior was unintentional because only the PATCH number of the version was incremented. If it was intended to break existing behavior I would expect the MAJOR number to be incremented.
Steps to reproduce
With the above code (or with an association defined in the same way), using gem version 0.8.4, obtain the output of the serializer.
Environment
ActiveModelSerializers Version: v0.8.4
Output of ruby -e "puts RUBY_DESCRIPTION"
: ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin18]
OS Type & Version: macOS 10.14.2
Integrated application and version (e.g., Rails, Grape, etc): Rails v5.1.6.1
Thanks for your report.
I'm curious, if you define 'last_sale' on 'Sale' if you get the same behavior.
Would be you be interested in helping write a failing test and possibly help fix the bug?
@rusterholz @bf4 I'm having the same problem.
To clarify how much time I'm going to put into this, I'm here more to help you help yourselves than to help you help me... without more work on your part to demonstrate and debug your problem, there's not much I can do.
If you're going to create and issue and disappear or comment on and issue without any details, nothing is going to happen.
I'm fairly busy, but I will try to get to this! My workload at my day job just jumped 50% due to team resizing. I've forked the repo but it may be a little bit before I can get around to writing a failing test. Until then, I thought it would be good to have the issue documented.