maliawelli

maliawelli

Hey,

I’ve got problem with has many association. I would like to connect posts with posts_likes through “post_id” and “user_id” (which would be took from guardian Guardian.Plug.current_resource(conn)). How can I do this? It is easily possible in SQL, but how to translate it to elixir and ecto?

In pseudo code it should look like:

SELECT posts, posts_favourites WHERE post.id = posts_favourites.post_id AND posts_favourites.user_id = Guardian.Plug.current_resource(conn).id

Showing Posts 29 to 20

NobbZ

NobbZ

You haven’t told anything about a logged in user before, anyway, that really doesn’t matter for your problem right now. We really have to keep it at a really basic level and should not overcomplicate it.

The main problems you have are iterating over a list while transforming each item into a list of separate results and how to deal with structs.

I have shown you how to do both in separate posts as you slowly uncovered your requirements. In germany we call this “salami tactics”, giving out stuff slice by slice…

In one post I introduced Enum.map/2 to you, in another I introduced the map/struct-update syntax. Combining those, as both are basic building blocks of elixir, is left as an exercise to you.

OvermindDL1

OvermindDL1

Views doesn’t matter.

In essence:
Is the remote server needing to authenticate with your server then Guardian is nice.
Are you auth’ing local, then Phoenix.Token is better.

maliawelli

maliawelli OP

@OvermindDL1

Actually, I use it for API. I don’t have any views. I authenticate user by JWT. So I store all logged in user data in:

Guardian.Plug.current_resource(conn)

OvermindDL1

OvermindDL1

Where do you store your logged in user data? I just use the build-in Phoenix.Token on the Plug.Session. Guardian is more useful for DB-less and remote API endpoints, not for a main website (it is significantly heavier than Phoenix.Token).

maliawelli

maliawelli OP

@NobbZ

I’m not sure though, what Guardian has to do with this…

If it is wrong, tell me how to get logged user data instead of Guardian? Mayby I’m wrong and there is simplier method.

You are right about boolean. So let’s assume:

field :is_favourite, :boolean, virtual: true

Still don’t have any idea how to iterate through posts to set this field true or false for each post. How can I achieve this?

@LostKobrakai
You are right. But with that code:
In posts_controller:

current_user = Guardian.Plug.current_resource(conn)
page = Post.preload
|> Post.order_asc_by_insertion
|> Post.getFavourites2(current_user.id)
|> Repo.paginate(params)

And in model:

def getFavourites2(query, user_id) do
from p in query,
left_join: f in “posts_favourites”, on: f.post_id == p.id and f.user_id == ^user_id
end

I don’t have posts_favourites in output.

LostKobrakai

LostKobrakai

Use left_join maybe?

NobbZ

NobbZ

Since the field is now part of your struct, you can use update syntax to set its value:

post = %{post | field: 1}

You can read that value by doing simple field access:

value = post.is_favourite

I’m not sure though, what Guardian has to do with this…


edit

I do consider the name (is_favourite) confusing for an integer field. In elixir the prefix is_ and the suffix ? are usually used for booleans, while the prefix is_ is meaning “this is guardsafe”.

maliawelli

maliawelli OP

@NobbZ:

I’m really thankful for your attention.

Ok, let’s make it simplier. How to get in each post information if post is favourite or not? Let’s use virtual field:

field :is_favourite, :integer, virtual: true

But how to set this? At first we need to check if user is logged, so if (Guardian.Plug.current_resource(conn)). Can I use Guardian.Plug.current_resource(conn) in model?
And later on, how to achieve is_favourite information?

maliawelli

maliawelli OP

@gregvaughn:
True/false indicator would be the way, but I don’t know how to achieve this. I would be appreciated for help.

And second option (with 2nd query) was posted above, but I still don’t know, how to join single post from posts list with that sql result.

gregvaughn

gregvaughn

I’m really confused at what you want. You want a true/false indicator in a list of posts you already have? Then either be really inefficient and keep what you have and just set to true if the count of results from get_favourites is 1, else false. Or else run a 2nd query to find all the user’s favourites and set a key on each matching post via an Enum.map

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
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
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews