josefrichter
Simple question re. code organization: do you do preloads in context, or in controller/liveview?
Say a basic function in context looks like this
def get_class!(id) do
Repo.get!(Class, id)
|> Repo.preload(:users)
end
But basically sometimes I need the association, sometimes I don’t.
So would you typically do get_class!(id) |> Repo.preload(:users) in controller/liveview? (I have a feeling that basically I should never call Repo… from controller/liveview)
Or would have separate get_class!/1 and get_class_with_assoc!/1 in context?
Or am I overthinking and it doesn’t really matter? Thank you.
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #elixirconf-us
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
stefanchrobot
I avoid calling repo from the controllers. My usual pattern is to do this in the context:
sodapopcan
Oooo, I’ve never thought about sending a preloads as a parameter. The only thing I don’t like this is that this gives LiveViews/Controllers some schema knowledge. I usually go the
get_posts_with_userroute. What’s your mileage been like with the extra argument, @stefanchrobot? I do like it in theory but I’ve only been thinking about it for 5 mins.stefanchrobot
I’m fine with the coupling, because the schema knowledge is already there - whenever you access associations via the root schema, you introduce the coupling:
In theory one could return
{foo, bar}from the context, but I don’t think it’s worth it. Plus in reality, most of the time, thebaris linked with that specificfooeither way and not just some free-floating entity.sodapopcan
In my mind, coupling when reading data structures and coupling when querying are different but actually, not really
That’s cool, thanks for the answer! I’m gonna try it out.
derek-zhou
Another way to do it is to not to use preload altogether. Just create views that include the association at the db level and treat them as separate schemas in ecto. So you will have the following schemas:
Then you just have different context functions as usual.
belaustegui
I would say that preloading in the controller is not bad and you should not worry about doing it.
If my controller needs to preload two associations because they are going to be displayed in a template this is a presentation concern. If the template change, I may not need this preloads, or I may need different ones.
There always is the option of passing a preload list to the context as was already mentioned in this tread. I agree with your concern that this would couple the controller with the schema and data model.
I think that at that point we are building an abstraction over Ecto just to avoid using the Repo module in the controller. We may as well call Repo.preload directly and remove an indirection layer.
In my opinion we should focus more about extracting business logic into the context than in blindly following dogmas such as “never use the Repo from the controller”.
My preferred way to doing this is having different functions for building the query in the context (those functions should have some real business logic that makes their extraction worthy).
Then, the controller can use those functions from the context and its own functions (for presentation concerns) to build the desired query and trigger it using the Repo.
For example the controller may want to
selecta subset of the fields since they are the only ones required in the template, orpreloadsome associations that are required for presentation. As long as the real business logic is extracted properly, this should be fine.If you follow this pattern you may see that some of the queries that you build in controllers may have common points such as
preloads. This is not bad per se, since tomorrow you can modify the controller or the template and know that you are not breaking any other part of the application. At that point, you will have enough information to decide if extraction is worthy or not.Locality of Behaviour is very valuable, and we usually don’t think about it much.
josefrichter
Could you please elaborate on this? Maybe short example in (pseudo)code? Not sure I fully understand.
derek-zhou
A view is a virtual table, see the postgresql doc here
If you define a view that flattens the foreign key association, you can just treat it as a normal table in ecto (you cannot insert into it though)
josefrichter
interesting. and you need to create the view using a migration with something like
create view (:classes_with_users)? Is that supported in ecto? Or do I need to create that manually directly in Postgres?dom