Code interface for resource owned by User

Now that I know what I’m trying to do, I still can’t figure out how. There is only one example in the documentation, and it does not how how to make use of the built-in options.

Here is the resource:

defmodule Red.Practice.Card do
  use Ash.Resource,
    data_layer: AshPostgres.DataLayer

  actions do
    defaults [:create, :read, :update]
  end

  code_interface do
    define_for Red.Practice

    define :create, action: :create
  end

  attributes do
    integer_primary_key :id

    attribute :word, :string, allow_nil?: false
    attribute :tried_at, :utc_datetime, allow_nil?: true
    attribute :retry_at, :utc_datetime, allow_nil?: true
    attribute :correct_streak, :integer, allow_nil?: false, default: 0

    create_timestamp :created_at
    create_timestamp :updated_at
  end

  identities do
    identity :unique_word, [:word, :user_id]
  end

  relationships do
    belongs_to :user, Red.Accounts.User,
      attribute_writable?: true,
      attribute_type: :integer,
      allow_nil?: false
  end

  postgres do
    table "cards"
    repo Red.Repo
  end
end

What I was able to make work

Red.Practice.Card.create!(%{
  word: "other",
  user_id: user.id
})

It’s not what I want because I had to make the belongs_to attribute_writeable?: true which I don’t need at this point (not yet assigning cards to someone else).

But it fails when

I try to remove the attribute_writable?: true and pass the actor option,

Red.Practice.Card.create!(%{
  word: "the"
}, actor: user)

Here is the error:

** (Ash.Error.Invalid) Input Invalid

* attribute user_id is required
    (ash 2.15.17) lib/ash/api/api.ex:2183: Ash.Api.unwrap_or_raise!/3
    /Users/dewet/code/ash/red/README.livemd#cell:x6riuxlbaf4k7urdmoqee6tneokxl3mt:20: (file)

How do I fix this?