graphiti icon indicating copy to clipboard operation
graphiti copied to clipboard

Sideposts fail when associating has_many resource with the `as:` option

Open dshoemaker opened this issue 2 years ago • 0 comments

I've set up a failing spec scenario on this branch here.

When given a relationship and resource setup like this:

class Employee < ApplicationRecord
  has_many :documents, class_name: "PersonalDocument"
end

class PersonalDocument < ApplicationRecord
  belongs_to :employee
end

class PersonalDocumentResource < ApplicationResource
  belongs_to :employee

  attribute :document_name, :string
end

class EmployeeResource < ApplicationResource
  has_many :personal_documents, as: :documents
end

This payload will fail when sideposting personal documents through the EmployeeResource with the error "No method 'personal_documents' on employee"

{
  data: {
    type: "employees",
    attributes: {first_name: "Joe"},
    relationships: {
      personal_documents: {
        data: [
          {type: "personal_documents", 'temp-id': "doc1", method: "create"}
        ]
      }
    }
  },
  included: [
    {
      type: "personal_documents",
      'temp-id': "doc1",
      attributes: { document_name: "Document 1"}
    }
  ]
}

This failure happens after all the models have been persisted and correctly associated. The association of the active record models works as expected, but the validation doesn't account for the as: option that is provided in the resource relationship.

This happens here

I'm happy to take a pass at putting together a pull request, but I wanted to confirm that my read of this issue is correct and that this functionality isn't intended.

Thanks!

dshoemaker avatar Jul 05 '22 14:07 dshoemaker