maliawelli
Has many through many fields
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
Most Liked
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):
posts => [post => [id => 1, title => 'test', 'user_id' => 2, 'comments' => ['user_id' => 2, 'post_id' => 1]]]
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):
for ($i = 0; $i < count(page.entries); $i++) {
page.entires[$i]['favourites'] = $this->getFavorite($post_id, $user_id);
}
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.
LostKobrakai
Use left_join maybe?








