Skip to content

Associations with namespaced models not working #916

Closed
@dpmccabe

Description

@dpmccabe

I have two models that come from a Rails engine, Spree::Product and Spree::Variant, and I can't figure out how to correctly specify an association between them in my serializers. Here is my current setup:

app/serializers/spree/product_serializer.rb
module Spree
  class ProductSerializer < ActiveModel::Serializer
    attributes :id, :name
    has_many :variants, serializer: Spree::VariantSerializer
  end
end
app/serializers/spree/variant_serializer.rb
module Spree
  class VariantSerializer < ActiveModel::Serializer
    attributes :id, :price
    belongs_to :product, serializer: Spree::ProductSerializer
  end
end

A serialization of products loads the attributes correctly, but the variants don't appear (not even an empty array). If I change has_many :variants, serializer: Spree::VariantSerializer to simply has_many :variants then I get the variants, but it includes all the default attributes instead of using VariantSerializer.

I've tried all sorts of combinations of file placement, class declarations, and associations to no avail.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions