Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sideposts fail when associating has_many resource with the as: option #426

Open
dshoemaker opened this issue Jul 5, 2022 · 0 comments
Open

Comments

@dshoemaker
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant