szajbus

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?

Showing Posts 1 to 5

blatyo

blatyo

Conduit Core Team

I’d probably reverse it and match on %Ecto.Association.NotLoaded{}, because it would be clearer to me.

thomasbrus

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

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 by followed_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

szajbus OP

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 :slight_smile:

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

szajbus OP

Yes, it’s another approach I consider. However, I think using pattern matching here, would be more concise and readable in this simple scenario.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
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
nseaSeb
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
kpanic
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
ryanwinchester
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 Top

JesseHerrick
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews