# Code interface for resource owned by User

**URL:** <https://forum.elixirforum.com/t/code-interface-for-resource-owned-by-user/58982>\
**Category:** Questions\
**Tags:** ash, ash-authentication, ash-code-interface\
**Created:** [October 15, 2023, 12:01am UTC](https://forum.elixirforum.com/t/code-interface-for-resource-owned-by-user/58982 "2023-10-15T00:01:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dewetblomerus](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dewetblomerus/32/27370_2.png) [@dewetblomerus](https://forum.elixirforum.com/u/dewetblomerus)\
**Post date:** [October 15, 2023, 12:01am UTC](https://forum.elixirforum.com/t/code-interface-for-resource-owned-by-user/58982/1 "2023-10-15T00:01:06Z")

</div>

I have a resource that `belongs_to` a User.

When I create one with my `code_interface` I pass in a `user_id`.

I want to start creating code interfaces for querying the resources belonging to that user, I can again pass in `user_id`.

However, when I look at the code generated by `ash_phoenix.gen.live` I see this beauty:

```elixir
  AshPhoenix.Form.for_create(Red.Api.Attempt, :create,
  api: Red.Api,
  as: "attempt",
  actor: socket.assigns.current_user
)

```

Instead of passing in the `user_id`, I just pass in the user as an `actor`.

What is the convention here? (Please don’t tell me to do whatever I feel like, I feel like learning the conventional/idiomatic Ash way of doing this).

If the convention is to pass in an `actor` in the code interface. Please share a link to an example if possible.

---

<div class="post-metadata">

**Author:** ![dewetblomerus](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dewetblomerus/32/27370_2.png) [@dewetblomerus](https://forum.elixirforum.com/u/dewetblomerus)\
**Post date:** [October 15, 2023, 12:02am UTC](https://forum.elixirforum.com/t/code-interface-for-resource-owned-by-user/58982/2 "2023-10-15T00:02:50Z")

</div>

I have not set up any policies or looked at those, if that or something entirely different is the conventional way of scoping a query to a specific user, I can go down that road too.

---

<div class="post-metadata">

**Author:** ![zachdaniel](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zachdaniel/32/31980_2.png) [@zachdaniel](https://forum.elixirforum.com/u/zachdaniel)\
**Post date:** [October 15, 2023, 2:22am UTC](https://forum.elixirforum.com/t/code-interface-for-resource-owned-by-user/58982/3 "2023-10-15T02:22:41Z")

</div>

The general pattern is to first ask yourself what kind of action you are writing. If you are writing the sort of action that is “contextual to the actor”, i.e. “create\_my\_thing”. vs “create\_thing\_for\_user”. They are subtly different, and in many cases you may have both. I.e some admin version of an action that allows supplying any user.

Generally speaking, your best bet is to _start_ by preferring to use the actor, and if later you determine that you want someone to be able to take that action “on behalf of” another user, you can either add another version of the action, or modify the internals of the action to expect an actor _or_ a supplied user\_id (for example).

All code interface functions accept an `actor` option, i.e `YourResource.create!(...., actor: actor)`.

So to sum up, prefer to write “actor-specific” actions (as you described), as they are easier to reason about.

---

<div class="post-metadata">

**Author:** ![dewetblomerus](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dewetblomerus/32/27370_2.png) [@dewetblomerus](https://forum.elixirforum.com/u/dewetblomerus)\
**Post date:** [October 16, 2023, 1:43am UTC](https://forum.elixirforum.com/t/code-interface-for-resource-owned-by-user/58982/4 "2023-10-16T01:43:34Z")

</div>

That is exactly what I needed 🚀 Thank you, Zack.

---

<div class="post-metadata">

**Author:** ![dewetblomerus](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dewetblomerus/32/27370_2.png) [@dewetblomerus](https://forum.elixirforum.com/u/dewetblomerus)\
**Post date:** [October 17, 2023, 12:59pm UTC](https://forum.elixirforum.com/t/code-interface-for-resource-owned-by-user/58982/5 "2023-10-17T12:59:55Z")

</div>

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](https://hexdocs.pm/ash/Ash.CodeInterface.html), and it does not how how to make use of the built-in options.

Here is the resource:

```elixir
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

```elixir
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,

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

```

Here is the error:

```elixir
** (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?

---

<div class="post-metadata">

**Author:** ![zachdaniel](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zachdaniel/32/31980_2.png) [@zachdaniel](https://forum.elixirforum.com/u/zachdaniel)\
**Post date:** [October 17, 2023, 1:37pm UTC](https://forum.elixirforum.com/t/code-interface-for-resource-owned-by-user/58982/6 "2023-10-17T13:37:31Z")

</div>

right, so now what you need to do is use the current user in the action to map it to the `user` relationship in some way.

The long way:

```elixir
change fn changeset, context -> 
  Ash.Changeset.change_attribute(changeset, :user_id, context.actor.id)
end

```

The built in way:

```elixir
# relate the actor to the `:user` relationship of this record.
change relate_actor(:user)

```

---

<div class="post-metadata">

**Author:** ![dewetblomerus](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dewetblomerus/32/27370_2.png) [@dewetblomerus](https://forum.elixirforum.com/u/dewetblomerus)\
**Post date:** [October 17, 2023, 2:34pm UTC](https://forum.elixirforum.com/t/code-interface-for-resource-owned-by-user/58982/7 "2023-10-17T14:34:41Z")

</div>

> [@zachdaniel](#):
>
> `change relate_actor(:user)`

Badabing Badaboom 💥

This worked:

```elixir
  changes do
    change relate_actor(:user)
  end

```

I was able to remove the `attribute_writable?: true` from the relationship.

And now this works:

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

```

---

<div class="post-metadata">

**Author:** ![zachdaniel](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/zachdaniel/32/31980_2.png) [@zachdaniel](https://forum.elixirforum.com/u/zachdaniel)\
**Post date:** [October 17, 2023, 2:45pm UTC](https://forum.elixirforum.com/t/code-interface-for-resource-owned-by-user/58982/8 "2023-10-17T14:45:52Z")

</div>

Ah, you likely want this to be only in a specific action, i.e

```elixir
create :create do
  change relate_actor(:user)
end

```

If it is in global changes, it will happen on all create _and_ update actions.

---

<div class="post-metadata">

**Author:** ![dewetblomerus](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dewetblomerus/32/27370_2.png) [@dewetblomerus](https://forum.elixirforum.com/u/dewetblomerus)\
**Post date:** [October 17, 2023, 2:54pm UTC](https://forum.elixirforum.com/t/code-interface-for-resource-owned-by-user/58982/9 "2023-10-17T14:54:57Z")

</div>

Oh gotcha! Thanks for clarifying. I did that and it works.

Here’s my whole Resource now, just in case someone else is wanting a complete example:

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

  actions do
    defaults [:read, :update]

    create :create do
      change relate_actor(:user)
    end
  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

```

I can’t guarantee that this file will keep living here, but if someone wants to see the up-to-date changes: [red/lib/red/practice/resources/card.ex at main · dewetblomerus/red · GitHub](https://github.com/dewetblomerus/red/blob/main/lib/red/practice/resources/card.ex)
