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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
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
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
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
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
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
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
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
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
maliawelli
Or if it’s not possible (but should be), I tried:
in model with getFavourites I download my favs for logged user. But this enum.reduce doesn’t work, could someone help me to fix this?
NobbZ
What do you mean by “doesn’t work”?
I do not see anything why it shouldn’t work, but probably it does “not return what you expect it to”.
user_idis static, you do not need to set it in the reduce-function, you can move it to the outside or directly useuser.id%{"favourite" => …}as its first element and the current item as tail.entry) and do not use it at all%{}, but then you make it an improper list, while this generally works it is inconsistentSo please show us some sample input (simplified), what your function returns and what you expect it to return instead.
This helps us to help you.
maliawelli
Thanks for your response.
Actually you are right. I do not get what I expect.
In my page.entries I’ve got something like (pseudocode):
I would like to join to each post my favourites from
%{"favourite" => Repo.all(Post.getFavourites(x.id, user_id))}I’m coming from OOP language.
For instance in PHP it would be (again pseudocode):
Probably it is easy, but I can’t get it in elixir.
My purpose is to have information in each post if logged user have post in favourites.
NobbZ
This is a bad idea. Pseudocode isn’t standardized and with natural languages, everyone can interprete some things slightly different than you.
This forum is about elixir, so it is best to use elixir when writing code. But of course you can use any language you want, but please use those that do actually exist and use them sparse.
To be honest, I do not understand what that piece of pseudocode-data is. Is it a keywordlist, a map, a record, something else? Why do you have keys sometimes in quotes and sometimes not?
What I requested you for was, that you provide us input, expected output, actual output and your implementation in a simplified way, so that we can load it into iex and play around with it.
Just like this:
This example is of course overly simplified and the problem obvios, but it is just an example.
But now, lets try to help you.
As I do understand you, you have a list of things, where each thing is a map (or struct).
You want to iterate over those things and collect a list of maps with one key each. That should be roughly possible like this:
But thats really only a quick guess.
Imperative for-loops which counter is used as an index in an assignment can often be translated into
Enum.map/2, which makes the code much clearer.maliawelli
You understood me clearly.
Here is my data structure:
In each post I would like to have information about “favourites” (only when user is logged), like for instance I’ve got information about comments, votes etc.
I modified your code a bit:
Probably it is fine, but I’ve got error:
And my getFavourites:
Can you help me please?
Or mayby there is better method to fetch this data?
gregvaughn
What’s happening here is the
selectingetFavouritesis returning a two element tuple with {post_id, user_id}. Poison doesn’t know how to convert tuples to json. You can get something serializable, but probably still needing to be tweaked for your needs with:select: %{post_id: p.post_id, user_id: p.user_id}because that will be a map that Poison can handle
NobbZ
The screenshot(?) shows some arbitrary JSON(?).
In your elixir code you will probably not deal with that JSON directly, but with an represantation in elixir terms. Its much more important how those look.
maliawelli
Thank you, you were right. I’ve so much to learn with elixir.
Anyway this enum.map posted above doesn’t return expected data. It returns only:
> “posts”: [ { “favourite”: }, { “favourite”: [ { “user_id”: 1, “post_id”: 2 } ] } ]
So all post data is thrown out. How can I fix this?
And mayby @gregvaughn do you have better idea to fetch this data?
@NobbZ
Yes, because I return .json data with:
Is that incorrect?
gregvaughn
You’re getting the data you are querying for. The problem appears to be your
getFavouritesquery. It doesn’t implement this (incorrect) sql you asked about.You probably want something more like:
Note: I haven’t tested that out because I don’t know your schema, but hopefully it’ll get you closer. I’m assuming you want full post data returned and not just ids like you are currently selecting
NobbZ
Which is as expected according how I understood you. This is why I asked for input and output as you expect it in elixir syntax multiple times. I’m still waiting.
Nope, but irrelevant as well… After encoding json it’s just a string from elixir point of view, but we are somewhere before that, we are dealing with maps, tuples, lists, integers, floats, atoms and whatnots. Those are relevant for us.