Ash default create action and keys

I have a question about the behavior of the default create action behavior.

I have a resource: Section, it has a uuid primary key and here is the relevant relationship declaration:

relationships do
    belongs_to :exam, App.MCAT.Exam, allow_nil?: false, writable?: true
    
    ...

  end

When I try to create a Section using the default create action like this:

Section.create!(%{exam_id: exam.id, type: :phys, order: 1, allotted_time_seconds: 60 * 95})

I get an Invalid Input error that says: “relationship exam is required”

I feel like including the id has worked for me before, but I think that is when the id was a primary key.

Is there a way to specify this field with the default create or do I need to make a custom create that accepts exam_id explicitly?

1 Like

The relationship being writable?: true refers to the relationship being usable with manage_relationship, which they are by default. What you are looking for is attribute_writable?: true, which allows writing to the generated attribute that underpins the belongs_to relationship.

2 Likes

Doh :man_facepalming:. Thank you! And you’re welcome to anyone who runs into this same thing :stuck_out_tongue_winking_eye:

1 Like