MartinVolerich

MartinVolerich

Ash Domain functions - best practices / approach

I have an Ash Domain called Flow.Model, that has a resource called Warehouse, that in turn has a relationship to many Tank(s).

The resource has relationships properly defined.

I want to provide a function in the domain that responds with the tanks for a warehouse.

A simple implementation is (of course):

 def get_tanks_from_warehouse(warehouse) when is_struct(warehouse, Flow.Model.Warehouse) do
    warehouse = warehouse |> Ash.load!(:tanks)
    warehouse.tanks
  end

Is this the best approach to use or is there a code_gen approach that can call an action to respond with the list of the “many” child resources?

Is it “wrong” to Ash.load the relationship this way in the plain Elixir function in the domain?

I’m keen to learn the best patterns to be using for the future.

Thanks
Martin

Most Liked

barnabasJ

barnabasJ

Ash Core Team

when you define a read action as a code interface you can also pass in a load

e.g. if you have

defmodule Domain do

  resource do
    resource Warehouse do
      define :get_warehouse, action: read, get_by: :id
    end
  end
end


Domain.get_warehouse(id, load: [:tanks]) 

That will give you the warehouse including the tanks

zachdaniel

zachdaniel

Creator of Ash

Nothing wrong with writing those functions, just up to you if you need the benefits of a generic action or not :smile: Functions are a simple tool and often using a simple tool is good. Since you’re aware of what you can get from generic actions, you can ultimately decide whether or not you need a generic action or not.

I personally error on the side of making generic actions and when I need functions they typically exist for a reason and I group them in some way to indicate that reason. Like if I have common helpers for building a UI I might put those in their own module MyApp.MyDomain.LiveViewHelpers or something along those lines. But when in doubt, finding a way to either extend an existing domain action or create a new one often yields better fruit down in the long run.

MartinVolerich

MartinVolerich

Very clear and practical. Many thanks again.

Where Next?

Popular in Questions Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
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
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New

We're in Beta

About us Mission Statement