dewetblomerus
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.
Trending in Questions
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Other Trending Topics
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #performance











Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
dewetblomerus
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.
zachdaniel
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
actoroption, i.eYourResource.create!(...., actor: actor).So to sum up, prefer to write “actor-specific” actions (as you described), as they are easier to reason about.
dewetblomerus
That is exactly what I needed
Thank you, Zack.
dewetblomerus
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:
What I was able to make work
It’s not what I want because I had to make the
belongs_toattribute_writeable?: truewhich 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?: trueand pass theactoroption,Here is the error:
How do I fix this?
zachdaniel
right, so now what you need to do is use the current user in the action to map it to the
userrelationship in some way.The long way:
The built in way:
dewetblomerus
Badabing Badaboom
This worked:
I was able to remove the
attribute_writable?: truefrom the relationship.And now this works:
zachdaniel
Ah, you likely want this to be only in a specific action, i.e
If it is in global changes, it will happen on all create and update actions.
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:
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