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
Trending in Questions
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
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
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
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
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
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
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
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
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
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
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
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
- #ai
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming











Showing Posts 29 to 20- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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/2to 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
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
@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:
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
@NobbZ
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:
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:
And in model:
I don’t have posts_favourites in output.
LostKobrakai
Use left_join maybe?
NobbZ
Since the field is now part of your struct, you can use update syntax to set its value:
You can read that value by doing simple field access:
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 prefixis_and the suffix?are usually used for booleans, while the prefixis_is meaning “this is guardsafe”.maliawelli
@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:
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
@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
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_favouritesis 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 anEnum.map