user20230119
Limit loading relationship of grandchild to grandparent
If I have relations like this:
blogs has_many posts
posts many_to_many users
blogs many_to_many users
How can I limit loading posts of users to only one blog?
blog (id 80) → users → posts of blog (id 80)
By default it loads all posts of users in every blog.
blog (id 80) → users → posts of all blogs
In Ecto, I can do this
Repo.get!(Blog, 80)
|> Repo.preload([
users: [posts: from(p in Post, where: p.blog_id == 80)]
])
Most Liked
zachdaniel
Creator of Ash
Api.get!(Blog, 80)
|> Api.load(users: [posts: Ash.Query.filter(blog_id == ^80)])
1
zachdaniel
Creator of Ash
There is not really a good way to accomplish this if I’m being honest. It’s not a pattern that would be easily generalizable to support all of the relationship features that Ash has. You’d potentially be able to accomplish this with a calculation on the parent object (blog) that produces a list of users and for each their posts in the parent blog. I.e
defmodule YourApp.Types.UserWithPosts do
use Ash.Resource, data_layer: :embedded
attributes do
attribute :user, :struct do
constraints [instance_of: User]
allow_nil? false
end
attribute :posts, {:array, :struct} do
constraints [items: [instance_of: Post]]
allow_nil? false
end
end
end
# in blog
calculate :users_and_posts, {:array, YourApp.Types.UserWithPosts}, YourCalculation
Where YourCalculation is a module that produces a list of those embedded types.
1
Popular in Questions
can someone please explain to me how Enum.reduce works with maps
New
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
Hi,
I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
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
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
The documentation above suggests that while ...
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Other popular topics
Hi All,
I set a environment variables in dev.exs , like below code.
when i start server, how can i set the ${enable} value?
thanks.
d...
New
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...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
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
Hi there,
I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









