diff --git a/plugin.rb b/plugin.rb index 75d3d92..6f714e5 100644 --- a/plugin.rb +++ b/plugin.rb @@ -689,7 +689,7 @@ SQL add_to_serializer(:user_card, :accepted_answers) do Post - .where(user: User.find_by_username('bar3')) + .where(user_id: object.id) .joins(:_custom_fields) .where(_custom_fields: { name: 'is_accepted_answer', value: 'true' }) .count diff --git a/spec/serializers/user_card_serializer_spec.rb b/spec/serializers/user_card_serializer_spec.rb new file mode 100644 index 0000000..56eafcd --- /dev/null +++ b/spec/serializers/user_card_serializer_spec.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe UserCardSerializer do + let(:user) { Fabricate(:user) } + let(:serializer) { described_class.new(user, scope: Guardian.new, root: false) } + let(:json) { serializer.as_json } + + it "accepted_answers serializes number of accepted answers" do + post = Fabricate(:post, user: user) + post.upsert_custom_fields(is_accepted_answer: 'true') + expect(serializer.as_json[:accepted_answers]).to eq(1) + + post = Fabricate(:post, user: user) + post.upsert_custom_fields(is_accepted_answer: 'true') + expect(serializer.as_json[:accepted_answers]).to eq(2) + end +end