Skip to content

record is received as a model class instead of an instance in the policy #136

Closed
@foxhard

Description

@foxhard

Hi,

I'm stuck with a thing related to received data from a POST request in a Policy.
I'm using jsonapi-authorization (3.0.2)

I hope you can help me. Thanks in advance 🙏

Problem description
After perform the API request, the record parameter in CarPolicy constructor will be received as a class of Car model instead of an instance of the same class so I can't access to the values sent from the API request like brand ("Audi") and model ("A4 Sedan") by using record.brand or record.model

Resource

class CarResource < BaseResource
  attributes :brand, :model
  
  def create?
    true
  end
end

Policy

class CarPolicy     
  def initialize(user, record)
    puts record.inspect #output: Car(id: integer, brand: string, model: string, created_at: datetime, updated_at: datetime)
  end
end

API Request
POST /api/cars

{
  "data": {
    "type": "cars",
     "attributes": {
         "brand": "Audi",
         "model": "A4 Sedan"
      }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions