szajbus
Hi, I’m wondering if the following is a viable approach.
def followed_by?(%{followers: followers}, user) when is_list(followers) do
followers
|> Enum.any?(fn follower ->
follower.id == user.id
)
end
def followed_by?(offer, user) do
offer
|> assoc(:followers)
|> where(user_id: ^user.id)
|> Repo.exists?()
end
Depending on the context, an offer may or may not have the followers association preloaded.
Pattern matching is used to check this, so that I can avoid unneeded database query when it actually is preloaded.
My doubt is about the guard: is is_list a good approach here?
It obviously works, because %Ecto.Association.NotLoaded{} is not a list and since it’s a has_many assoctiation, it produces a list when loaded.
But how about the readability/understandability?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
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
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
blatyo
I’d probably reverse it and match on
%Ecto.Association.NotLoaded{}, because it would be clearer to me.thomasbrus
There is also
Ecto.assoc_loaded?/1. But you can’t use it as a guard.Also in terms of readability
def followed_by?(%Offer{followers: .. }is a bit easer to follow I think.thomasbrus
Another thing to think about.. I think the first function knows a bit too much.
It reaches into the
%Offer{}which happens to have followers preloaded, and thats why it works. Imagine%Offer{}had other associations that may or may not be preloaded but are needed byfollowed_by?. Then the pattern matching would have to take into account all these different scenarios.Perhaps you can accomplish this in a different way (while still efficient). Let’s say you have a show page on which you show 1 offer. Then you can simply use the
followed_by?(offer, user)you already have (2nd function).But then perhaps there’s an index page with a list of offers which preloads
followers(I assume). In that case I would perform an additional query:offers_followed_by_user(user). It can just return the ids of the offers for example. Then it is straightforward and efficient to check if a certain offer on the index page is followed by the user (offer.id in offers_followed_by_user).szajbus
I like this.
In fact, I went for a walk right after posting this question and returned home with exactly same idea in mind. I guess time away from keyboard pays off
It is more direct than using
is_list, which seemed a bit too clever.Although it seems to be reaching a bit too deep into Ecto’s internals, I think it’s safe - any breaking change in Ecto in this regard, would be most likely caught at compile-time (undefined struct).
szajbus
Yes, it’s another approach I consider. However, I think using pattern matching here, would be more concise and readable in this simple scenario.