Can I fetch a resource with `get :id`?

To fetch a resource by a specific id I use this code:
[post] = MyApp.Post
|> Ash.Query.filter(id == "bc38fcb3-080d-46ba-834d-5as58d689sda")
|> Ash.Query.limit(1)
|> MyApp.Api.read!()
[post] = MyApp.Post
|> Ash.Query.filter(id == "bc38fcb3-080d-46ba-834d-5as58d689sda")
|> Ash.Query.limit(1)
|> MyApp.Api.read!()
Is there a shorter already build in way of fetching a single resource by it's id?
3 Replies
ZachDaniel
ZachDaniel3y ago
Yep! MyApp.Api.get!(MyApp.Post, id)
waseigo
waseigo3y ago
TIL!
ZachDaniel
ZachDaniel3y ago
It uses the primary read action, so it is effectively equivalent to the code snipept provided by @Stefan Wintermeyer .

Did you find this page helpful?