dewetblomerus

dewetblomerus

Code interface for resource owned by User

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:

  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.

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

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:

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

The built in way:

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

Also Liked

zachdaniel

zachdaniel

Creator of Ash

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.

dewetblomerus

dewetblomerus

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:

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

dewetblomerus

dewetblomerus

That is exactly what I needed :rocket: Thank you, Zack.

Last Post!

dewetblomerus

dewetblomerus

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:

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

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement